I have to match patterns from a main string using regex in java 8
This is the pattern I have so far.
Email.*?(:parameter[0-9] [^,])
It works on line 1 and line 2 below but fails on line 3 by matching just this Email IN (:parameter10
Note: I am fine with the closing bracket at the end being matched or not, I can work either way
// should match "Email = :parameter1)"
String line1 = "(Email = :parameter1)";
// should match "Email IN (:parameter1,:parameter2)"
String line2 = "(Email IN (:parameter1,:parameter2) AND (FirstName = :parameter3))";
// should match "Email IN (:parameter10,:parameter11)"
String line3 = "(Email IN (:parameter10,:parameter11) AND (FirstName = :parameter13))";
Thanks in advance
CodePudding user response:
You can use
Email.*?(:parameter[0-9] )(?![0-9,])\)?
See the regex demo. Details:
Email
- a fixed string.*?
- any zero or more chars other than line break chars as few as possible(:parameter[0-9] )
- Group 1: a:
char, thenparameter
word and then one or more digits(?![0-9,])
- a negative lookahead that fails the match if there is a digit or a comma immediately to the right of the current location\)?
- an optional)
char.
CodePudding user response:
Based on your input, technically this is sufficient:
Email[^)]*\)
It takes everything for Email up to the last ) inclusive.
If you want more validation on the parameterX then this is more specific
Email.*?((:parameter\d ,?) )\)
It takes Email then anything until first parameter then optional other parameter and again ends by the )