Jaycee
Jaycee

Reputation: 3118

I wish to fix this regex to match both blocks of text separately

@@IF[- (<>%='@a-z0-9\r\n\t).:!_,+/""]+(@@END IF|@ENDIF)

Hello I wish to fix the above regex to match both blocks of text separately using C#. At the moment it treats the below as a single match.

@@IF (<%=SHOPCODE%> == '2' ) THEN
return complicated if 
@@ELSE
Boo
@@END IF 

!

@@IF (<%=SHOPCODE%> == '1' )  THEN
County is UK
@@ELSE
Not UK
@@END IF

Upvotes: 1

Views: 64

Answers (2)

Braj
Braj

Reputation: 46841

You can try capturing groups along with Lazy way.

(@@IF\b[\S\s]*?\r?\n@@?END ?IF)

Live Demo

Regex explanation: (as per your need you can change optional)

  (                        group and capture to \1:
    @@IF                     '@@IF'
    \b                       the word boundary
    [\S\s]*?                 any character (0 or more times (least possible))
    \r?                      '\r' (carriage return) (optional)
    \n?                      '\n' (newline)
    @                        '@'
    @?                       '@' (optional)
    END                      'END'
     ?                       ' ' (optional)
    IF                       'IF'
  )                        end of \1

Upvotes: 3

Lucas Trzesniewski
Lucas Trzesniewski

Reputation: 51330

Another case of greediness...

Use non-greedy quantifiers (and simplify the regex):

@@IF\s+.*?(?:@@END IF|@ENDIF)

Demo here: http://regex101.com/r/kV0tM5/1

The part .*? will match as few characters as possible.

Upvotes: 1

Related Questions