0

From this file:

xxxxxxx; foo("this should be extracted 1"); bar("this should not be extracted"); yyyyyyy zzzzzzzzzzzzzzzzzzzzzzzz foo("this should be extracted 2") uuuuuuuuuuuuuuuuuuuuuuuuuuuuu 

I want to get this:

this should be extracted 1 this should be extracted 2 

I only managed to select the strings I want to extract using this regex: (?<=foo\(")(.*?)(?="\)). But I can't find a way to extract it or to delete all the unmarked text.

I put some x, y, z and u characters in the example just to show that in this file there are a lot of other things in the same line

Could somebody help me?

Thanks!

2 Answers 2

1

You can use ^(?:(?!foo\(\").)*$ to delete all lines not containing foo("

Then ^\r\n to remove empty lines

And then use ^.*foo\(\"(.*?)\"\).* to replace all with \1

2
  • Ctrl+H
  • Find what: (?:^|\G).*?foo\("(.+?)"\)(?:(?!\bfoo\().)*
  • Replace with: $1
  • check Wrap around
  • check Regular expression
  • UNCHECK . matches newline
  • Replace all

Explanation:

(?:^|\G) # beginning of line OR restart from last match position .*? # 0 or more any character but newline, not greedy foo\(" # literally (.+?) # group 1, 1 or more any character but newline not greedy, what to be extracted "\) (?:(?!\foo\().)* # Tempered greedy token, make sure we haven't "foo" after 

Replacement:

$1 # content of group 1, what to be extracted 

Result for given example:

this should be extracted 1 this should be extracted 2 

Screen capture:

enter image description here

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.