2

I have input string

DISPLAY_MSG='Kumar + sajdhjhasd - Type' 
              and  ID=156090 
              and RESOURCE_KEY='Ascend.ElementMaster.kumar_type.desc' 
              and  LOCALE='en_US'

I want to match only DISPLAY_MSG='Kumar + sajdhjhasd - Type' but using this expression

DISPLAY_MSG='[\\w\\W\\s\\S]*

matching whole string. How can I select only string between only those two single codes after

DISPLAY_MSG=
Preet Sangha
  • 64,563
  • 18
  • 145
  • 216
Pokuri
  • 3,072
  • 8
  • 31
  • 55

3 Answers3

3

This regex is quite nonsensical.

[\w\W\s\S] means: Match a character if it is either alphanumeric or non-alphanumeric or whitespace or non-whitespace. The exact same result can be achieved by (?s)..

Only in JavaScript (where the (?s) option that allows the dot to match newlines isn't available, it makes sense to write [\s\S] instead. But [\w\W\s\S] is definitely overkill.

So, a better solution using lazy quantifiers would be

DISPLAY_MSG='(?s).*?'

But even better would be to actually specify what is allowed between the quotes, and that usually is anything but a quote:

DISPLAY_MSG='[^']*'
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
2

You should try using non-greedy quantifier to reduce the match to smallest possible length.

RegEx: Smallest possible match or nongreedy match

Following is a good example http://www.exampledepot.com/egs/java.util.regex/greedy.html

Community
  • 1
  • 1
Rajendran T
  • 1,513
  • 10
  • 15
2

You should use ungreedy quantifier. Try this:

DISPLAY_MSG='[\\w\\W\\s\\S]*?'
Igor Konoplyanko
  • 9,176
  • 6
  • 57
  • 100
  • Thanks, your solution working... but I made little change to my expression to include and as well to replace from DSIPLAY_MSG to it's next and(this and can occur one or more times) DISPLAY_MSG='[\\w\\W\\s\\S]*?'[\\s]*and{0,1}. but it was not matching. What could be the case? – Pokuri Jan 16 '12 at 09:14