开发者

Print certain part in a Regular Expression?

开发者 https://www.devze.com 2023-04-01 01:33 出处:网络
For example, a file contains multiple 开发者_StackOverflow社区strings, where some look like this:

For example, a file contains multiple 开发者_StackOverflow社区strings, where some look like this:

--EX "ABCDEFG"

I created the following regular expression to see if the line matches:

^--EX\ \".*\"$

My question is now, can I somehow extract the content of .* in order to just get ABCDEFG? Are regular expressions suitable for this?


Put parentheses around the part you want to get.

^--EX\ \"(.*)\"$

Then in whatever your code, it will be match 1, most implementations of regex imply match 0 is the full match, and match 1 will be the first set of parenthesis, and so on.


you can get it via sed:

$ echo "--EX "ABCDEFG"" | sed 's/^--EX \(.*\)$/\1/'
ABCDEFG


On the command line you must enter your command precisely as shown here:

sed -e "s/^--EX \"\(.*\)\"$/\\1/g" filename
0

精彩评论

暂无评论...
验证码 换一张
取 消