Dan
Dan

Reputation: 11

POSIX regexp for finding everything between wordX and wordY

I have string as:

FOO /some/%string-in.here BAR

I would like to get everything between FOO and BAR but NOT including FOO[:space:] and NOT including [:space:]BAR

Any ideas it will be appreciate it.

Upvotes: 1

Views: 271

Answers (2)

Jan Goyvaerts
Jan Goyvaerts

Reputation: 22009

To get everything between FOO and BAR, except any spaces after FOO or before BAR, use this regex for POSIX ERE:

FOO[[:space:]]*(.*)[[:space:]]*BAR

or for POSIX BRE:

FOO[[:space:]]*\(.*\)[[:space:]]*BAR

Because POSIX only supports greedy quantifiers, this regex captures everything between FOO and the last occurrence of BAR, in case there is more than one BAR.

Upvotes: 0

harto
harto

Reputation: 90513

Can't you just use a capturing group?

/FOO (.+?) BAR/

Upvotes: 0

Related Questions