Home > OS >  REGEX : Match after A to the last B but only before C
REGEX : Match after A to the last B but only before C

Time:04-29

I would like to match everything after \t(\fn but only before \and excluding the last bracket that belongs to the \(t (as you can see on the attached picture)

For this :

\t(\fnJester (BIG))
\t(\fnJester (BIG))\i1
\t(\fnJester (BIG)
\t(\fnJester))))\fnArial (BOLD)\

I want to match :

Jester (BIG)
Jester (BIG)
Jester (BIG
Jester)))) and Arial (BOLD

I'm almost there with this pattern :

(?<=\\t\(.*?\\fn).*(?=\)|\\)

But since it's greedy, it matches everything even after \

brackets to ignore

CodePudding user response:

You can match the regular expression

(?<=fn)[^\\]*(?=\)|\\fn)

Demo

The expression can be broken down as follows.

(?<=     # begin a positive lookbehind
  fn     # match 'fn'
)        # end positive lookbehind
[^\\]*   # match zero or more chars other than '\'
(?=      # begin a positive lookahead
  \)     # match ')'
  |      # or
  \\fn   # match '\fn\
)        # end positive lookahead

CodePudding user response:

You may use this regex:

(?<=\\fn).*?(?=\\fn|\)(?:\\(?!fn)|$))

Updated RegEx Demo

(?=\\fn|\)(?:\\(?!fn)|$)) is the lookahead condition that is making sure that our match is ending with \fn or ) followed by optional \.

  • Related