MechMK1
MechMK1

Reputation: 3378

Print certain part in a Regular Expression?

For example, a file contains multiple 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?

Upvotes: 1

Views: 97

Answers (3)

Matthew
Matthew

Reputation: 25763

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.

Upvotes: 2

Jack
Jack

Reputation: 1525

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

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

Upvotes: 1

Michał Šrajer
Michał Šrajer

Reputation: 31182

you can get it via sed:

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

Upvotes: 1

Related Questions