Print certain part in a Regular Expression?
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
精彩评论