klaasjan69
klaasjan69

Reputation: 191

Regex pipe needs to take first match

My regular expression is like this:

      .*(kgrj4e|\*)[^:]*:([^;]*);?

The 'kgrj4e' part is a userid and is dynamic. The PR.... parts are printers. If the userid is not found I want the default printer (PR12346).

For first test string below I want result to be PR12345, but I get PR12346

snljoe,snlaks,kgrj4e,snlbla:PR12345;*:PR12346

Note: the users snljoe, snlaks and snlbla are just examples and can be totally different. In fact the list of users can be longer or smaller.

For second test string below I want result to be PR12346

snljoe,snlaks,snlbla:PR12345;*:PR12346

How to fix the regular expression so both test strings give the expected result?

Upvotes: 1

Views: 232

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626738

You can get the number with a search and replace:

    Search for: ^(?:(?!.*,kgrj4e(?:[;,])).*\*:(\w+)|.*?(PR\d+).*)
Replace with: $1$2

See this demo

I assume that the kgrj4e is a user-defined value that should be missing in the string to match the last printer value. If it is present, the first printer value is returned.

Upvotes: 1

Related Questions