0

[Posting this question because I could not find any question matching my scenario, please point me to the post if this is already discussed, I will delete this post.]

Trying to create a regex to match string app=myApp in long string separated by either , or ;.

My regex fails if the patterns is at end and not terminated by by either , or ;.

This is the regex I have used: [^.][app|APP]=(.*?)[,|;] this works for the following strings:

  1. env=prod;app=myApp;app.secure=yes
  2. app=myApp;app.secure=yes

But does not work for following:

  1. env=prod;app=myApp
  2. app=myApp

Here is my code:

 Pattern pattern = Pattern.compile("[^.][app|APP]=(.*?)[,|;]");
 Matcher matcher = pattern.matcher(stringVar);
 if (matcher.find()) {
      return matcher.group(1);
 }

I have also tried: [^.][app|APP]=(.*?)[,|;|$] but still no luck.

Kishor Prakash
  • 8,011
  • 12
  • 61
  • 92

1 Answers1

1

Try Regex: (?:app|APP)=(.*?)(?=,|;|$)

Demo

Matt.G
  • 3,586
  • 2
  • 10
  • 23