is_oz
is_oz

Reputation: 973

Regex may contains a group or not

I have multiple string to parse. This text could be multiline or not. Also, some part may not be exist. I have some samples to understand what I need.

Samples;
1-singleline) 00026A123456123456789012741852
2-multiline) 00030A789ABC210987654321258369X123

X is seperate groups.
I try to use this regex: (?<group1>.*)(?:[X](?<group2>.*))

Upvotes: 1

Views: 1099

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626870

If there can be only 1 X separating the groups, or it is the first X that always separates the groups, you may use

^(?<group1>.*?)(?:X(?<group2>.*))?$

See the regex demo.

The first group pattern should be a lazy dot .*? and the second one should be wrapped with an optional non-capturing group (?:....)?.

When the text has no X..., the second capturing group is considered non-participating, and thus is either null or empty (depending on where you use the regex).

Details:

  • ^ - start of string
  • (?<group1>.*?) - any 0+ chars (or excluding linebreak symbols, depending on the regex engine) as few as possible up to the first
  • (?:X(?<group2>.*))? - an optional sequence of X followed with any 0+ chars as many as possible up to
  • $ - the end of string.

Upvotes: 2

Related Questions