2

I am trying to match String messages that doesnt starts with String Line or line but has the substrings TIMEKEEPER,CLIENT and LAW FIRM using the below RegEx

def regEX = "^((?!(Line|line)+).).?(\\bTIMEKEEPER\\b|\\bCLIENT\\b|\\bLAW FIRM\\b|).*\$"

Below are some Sample messages.

Line 1 : Missing required fields //returns false as expected
line 2 : No Delimiter //returns false as expected
PL is not approved TIMEKEEPER //returns true as expected
CLIENT  ABC is not authorized  //returns true as expected
LAW FIRM address is required //returns true as expected

But the issue is, above RegEX is just matching all the messages that doesnt start with String Line or line only, its not checking for the matching substrings TIMEKEEPER,CLIENT and LAW FIRM in the message. ABove RegEX shouldn't match below messages but the RegEX returning true for below messages too, Can someone please help me fixing the RegEX to not to match below type of messages.

address is required //returns true instead of false
ABC is not authorized //returns true instead of false
OTUser
  • 3,788
  • 19
  • 69
  • 127

2 Answers2

3

You may use a regex like this (defined with a slashy string):

def regEx = /^(?![Ll]ine\b).*\b(TIMEKEEPER|CLIENT|LAW FIRM)\b.*$/

See the regex demo

Details

  • ^ - start of string
  • (?![Ll]ine\b) - no whole word Line or line allowed at the start
  • .* - any 0+ chars as many as possible
  • \b(TIMEKEEPER|CLIENT|LAW FIRM)\b - any of the 3 whole words in ALLCAPS
  • .*$ - the rest of the line.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • You may also use a case insensitive modifier and write the pattern as [`/(?i)^(?!line\b).*\b(TIMEKEEPER|CLIENT|LAW FIRM)\b.*$/`](https://ideone.com/0YcCeq) – Wiktor Stribiżew Oct 29 '17 at 21:11
  • Awesome, I was about to ask for case sensitive RegEx, thanks you so much, I really appreciate ur effort – OTUser Oct 29 '17 at 21:16
  • can you please help me with this problem https://stackoverflow.com/questions/47717505/groovy-create-a-map-with-jax-b-objects-specific-attributes – OTUser Dec 08 '17 at 19:36
0

Are you trying to match all the substrings TIMEKEEPER, CLIENT, and LAW FIRM, or at least one? This seems like an abuse of regular expressions, frankly, when Java has a perfectly good String#startsWith() method. You're simply looking for a small set of literal substrings here.

AbuNassar
  • 1,128
  • 12
  • 12