-2

I have a simple example as follows

user=djdjdjjdjdjdj; user=jdjdjdjdjdj;

And

user=djdjdjjdjdjdj;user=jdjdjdjdjdj;

Differences in white space between the two examples above. When I used Regex to separate user codes, it ok with first example. Can you advise me how to do better, this is my Regex code:

(?<=user=)([^\s]+)(?=;)

Cosi Nguyen
  • 13
  • 1
  • 3

2 Answers2

2

I would just use here:

(?<=user=)[^;]+

Demo

This would match each user value, sans semi colon, which appears in the input. If you really want to use capture groups, then I suggest not even using the lookbehind:

\buser=([^;]+)

This would place each user value in the first capture group.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

If you want to match [^\s] but also a space, simply use ..

So your regex would be (?<=user=)(.+?)(?=;) (with a non greedy quantifier to match until the next character is a ;).
Or you can match until the first ;, use this : (?<=user=)([^;]+)

Seblor
  • 6,947
  • 1
  • 25
  • 46