Nirmal Srinath
Nirmal Srinath

Reputation: 31

how to add conditions for regular expression?

I have to get the string which start with =" and ends with next ". But it should contains < symbol.

(="([^"])*<*")

String:

dit niet "dit wel" dit ook niet ="maar <dit ""en""dit ook"
REM dit niet "dit <wel" dit ook niet ="maar dit ""en""dit ook"
dit niet "dit wel" REM "maar dit ""en""dit ook"

The above code give me two string, I need to get only one mentioned below.

Expected Result:

="maar <dit "

Actual Result:

="maar <dit "
="maar dit "

NOTE: Need to get all the results which start with =" and have < and ends with next occurrence of "

Upvotes: 3

Views: 1874

Answers (2)

Thomas Ayoub
Thomas Ayoub

Reputation: 29441

Switch your regex to

(="([^"]*<[^"]*)*")

Live here

This part ([^"]*<[^"]*) ensure that your inner string will contain <

Upvotes: 2

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626927

Your ="([^"])*<*" regex matches =", then it captures any character other than " repeatedly (so that only the last occurrence of it is saved in the Group 1 buffer), and then zero or more < symbols followed with ". So, your regex does not really require a < to be present in the string you match.

You may use

="([^"<]*<[^"]*)"

See the regex demo

Details:

  • =" - a =" sequence
  • ([^"<]*<[^"]*) - Group 1 that will hold the value:
    • [^"<]* - zero or more chars other than " and <
    • < - a < symbol
    • [^"]* - zero or more chars other than "
  • " - a double quote

Upvotes: 3

Related Questions