Ghita
Ghita

Reputation: 4505

Regex - optional non capturing group not matched

I try to match from "Some other data. FlowSessionId: bf4a15d2-86ec-4717-8acc-f7d5229f250f additional data" using the regexp:

.*?(?:FlowSessionId:\s([\S]+))?.*

The flowSessionId information group is optional. When that is present I extract "bf4a15d2-86ec-4717-8acc-f7d5229f250f" data.

The problem is that when I try to make the group optional (using ? at the end of the non capturing group) I will not receive a group match anymore. See the example here: https://regex101.com/r/FU4IkM/1

Upvotes: 0

Views: 694

Answers (2)

The fourth bird
The fourth bird

Reputation: 163217

You can omit the non capture group and the square brackets, and either match the part with the capture group, or match the whole line.

.*?\bFlowSessionId:\s(\S+).*|.+

The pattern matches:

  • .*?\bFlowSessionId:\s Match as least as possible chars and then FlowSessionId:
  • (\S+).* Capture 1+ non whitespace chars in group 1 followed by the rest of the line
  • | Or
  • .+ Match 1+ times any char

Regex demo

const regex = /.*?\bFlowSessionId:\s(\S+).*|.+/;
[
  "Some other data.FlowSessionId: bf4a15d2-86ec-4717-8acc-f7d5229f250f additional data",
  "Some other data. additional data"
].forEach(s => {
  let m = s.match(regex);
  console.log(m[1] ? m[1] : m[0]);
})

Upvotes: 2

Peter Thoeny
Peter Thoeny

Reputation: 7616

Use a positive lookbehind to match only if the FlowSessionId: text exists:

(?<=FlowSessionId:\s)(\S+)

Explanation:

  • (?<=FlowSessionId:\s) - positive lookbehind for the session ID label; if this fails, the whole regex fails
  • (\S+) - match and capture the ID (if the lookbehind is a match)

Note that some browsers, notably IE do not support lookbehind.

Upvotes: 0

Related Questions