Home > Net >  Regex for last match of string in last group
Regex for last match of string in last group

Time:12-29

I have following string.

SO 01 053 675 A2

And Here I need

SO to be in Group 1 (Group 1 can have at most 2 characters).

01 053 675 in Group 2. (Group 2 can have space, comma, forward slash, dot, dash)

A2 in Group 3 (Group 3 can have one alphabet at most and one number at most).

And above groups are seperated by space and/or dash or without seperator.

I have prepared following.

^([A-Z|a-z]{2})*[-|\s]*([A-Z]{0,2}[\d|,|\/|.] )*[-|\s]*([A-Z]?[\d]?)

Now above regex works as far as Group 2 doesn't have space.

e.g. WO 01.053/675 A2

But it doesn't work with space in Group 2 even if I add space in 2nd group in regex.

e.g. WO 01 053 675 A2

How can I fix this regex?

Below I have added sample. You can see that A2 doesn't map to Group 3 at all when space is there.

https://regex101.com/r/6FceHh/1

Following are all sample string which requires support as individual solution is breaking other things.

In following case OK should go in Group 1. Middle string should go in Group 2. Last string A1 should go in Group 3.

'OK-' // Here Group 2 and Group 3 will be empty.
'OK-AB1234-A1'
'OK-1234-A1'
'OK-112-223-A1'
'OK 112 223 A1'
'OKB1A1'
'OKBB1A1'
'OK112223A1' // Here no separator is present.
'112.223' // Here Group 1 and Group 3 will be empty.
'112 223' // Here Group 1 and Group 3 will be empty.

CodePudding user response:

Using three optional groups with any [- ] separator in between turned out to work.

^\b([A-Za-z]{2})?[- ]*([A-Z]{0,2}[\d,\/. -]*\d)?[- ]*([A-Z]\d?)?

Here is the demo at regex101 - The \b word boundary will prevent empty matches.

CodePudding user response:

You could try this: ^(\w{2})\s(.*)\s(\w{2})$

  • Matches the first segment / 2 characters: (\w{2})
  • Everything in the middle: (.*)
  • And the last 2 characters: (\w{2})
  • Related