Nicholas Wood
Nicholas Wood

Reputation: 23

Regex to return full string if pattern not matched

I need a regex to interpret the data from a product barcode.

The following works for my use case:

(\w+)\^\^(\w+)\^(\w+)

with example barcode

PRODID^^BATCH^EXP 

returning these three elements as separate groups.

What I would like is is a barcode is presented that is not in the A^^B^C format then the full string is returned as group one.

Upvotes: 1

Views: 347

Answers (2)

user557597
user557597

Reputation:

The logical choice is to use a Branch Reset

(?m)^(?|(\w+)\^\^(\w+)\^(\w+)|(.+)()())$

https://regex101.com/r/mFoOdW/3

Explained:

 # Barcode regex
 # -----------------------
 (?m)                          # Multi-line mode
 ^                             # Beginning of line
 (?|                           # Branch reset
      ( \w+ )                       # (1), Element 1
      \^\^
      ( \w+ )                       # (2), Element 2
      \^
      ( \w+ )                       # (3), Element 3
   |                              # or,
      ( .+ )                        # (1), Entire line
      ( )                           # (2), empty
      ( )                           # (3), empty
 )
 $                             # End of line

If you expect optional padded characters before/after the elements,
you could use it with this modification instead.

(?m)^(?|.*?(\w+)\^\^(\w+)\^(\w+).*?|(.+)()())$

https://regex101.com/r/mFoOdW/4

Upvotes: 2

CrayonViolent
CrayonViolent

Reputation: 32532

Here's my take on it..

^((?=\w+\^\^\w+\^\w+)\w+|.*)(?:\^\^(\w+)\^(\w+))?

Group 1 makes use of positive lookahead to see if your a^^b^c format matches, and falls back to just matching all. Meanwhile, the additional ^^b^c stuff is wrapped in a non-capturing group and made optional.

demo (thanks @Barmar !) https://regex101.com/r/tI7QV2/2

"a^^b^c"

Full match  0-6 `a^^b^c`
Group 1.    0-1 `a`
Group 2.    3-4 `b`
Group 3.    5-6 `c`

"abc"

Full match  0-3 `abc`
Group 1.    0-3 `abc`

Upvotes: 3

Related Questions