Skip to content Skip to sidebar Skip to footer

What's Wrong With The Non Capture Group In My Regular Expression

I'm trying to write a regular expression that will match a strings similar to the ones below: Yu MSBE26 w AWAQBNL I am using Javascript and have come up with the following regular

Solution 1:

Remove the second [AWMS\d]{2} - it looks like an accidental addition and is the reason your regex doesn't work:

(.*?(?:[AWMS\d]{2})[A-Z]{2}[\dA-Za-z]{1,3})

Edit: you don't even need the non capture group, the square brackets are enough:

(.*?[AWMS\d]{2}[A-Z]{2}[\dA-Za-z]{1,3})

Solution 2:

Your regex doesn't match your values because simply they don't match.

Your pattern is:

(.*?(?:[AWMS\d]{2})[AWMS\d]{2}[A-Z]{2}[\dA-Za-z]{1,3})

Yu MSBE26
     ^--- fails here
w AWAQBNL
     ^--- fails here

Btw, you can use your regex to match your strings as this:

(.*?[AWMS\d]{2}[A-Z]{2}[\dA-Za-z]{1,3})

Working demo

Post a Comment for "What's Wrong With The Non Capture Group In My Regular Expression"