简体   繁体   中英

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.

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`

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM