GriGrim
GriGrim

Reputation: 2921

Regex: substring in another substring

I have the following text:

query([[
select *
from ]]..s_Schema..[[Table
where RowNumber = n_Counter
]],{Counter = n_Counter});

I need to find substring n_ that is between ([[ and ]],.

Such a regexp: \(\[\[[.\r\n]+n_[.\r\n]+\]\], doesn't work.

My case seems to be opposite to this one: Regular expressions: find string without substring

Or this: regex match substring unless another substring matches

Upvotes: 0

Views: 389

Answers (2)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522181

A literal implementation of your requirement would be:

Find:

\(.*\[\[.*?(n_\w+).*\]\]

Your current regex has several problems, of which perhaps which stands out the most is this:

[.\r\n]

This character class doesn't do what you think; this will match one of the following three things: a literal dot, a carriage return, or a newline. The query code you are trying to match will never match to this.

Demo here:

Regex101

Upvotes: 1

Lars Fischer
Lars Fischer

Reputation: 10199

Do a regular expression find like this:

  • Open Find Dialog
  • Find What: \(\[\[\K.*(?=\]\],)
  • Check regular expression
  • Check . matches newline
  • Click Find Next

Explanation

  • \(\[\[\K starts matching after [[
  • (?=\]\],) is a Lookahead, it matches only if followed by ]],

Upvotes: 0

Related Questions