Search code examples
regexgoregex-lookaroundsregex-groupregex-greedy

How to match these string variations in regex?


The question is pretty straightforward, I want to process a string using regex compiler in Go, and break it apart into three substrings. I already have something that works but sadly it has some shortcomings, this is my regex expression

str := "ARM64x99Bar"
pattern := `^(?!^\d+$)[a-zA-Z0-9]+(\d+)(.*)$`

r := regexp.MustCompile(pattern)
matches := r.FindStringSubmatch(str)

for example, Foo9Bar works and I can obtain the three desired substrings, Foo, 9, Bar, however this breaks if there are double digits, for example, Foo99Bar returns, Foo9, 9, Bar, firstly, how can I improve upon this?

Secondly, it gets complicated when the string is for example, ARM64x99Bar, in this case, I would like to obtain ARM64x, 99, Bar as well.

So, in summary, the first substring group can be alphanumeric or words but can never start from numbers, the second substring group will always be numeric (one or more digits, max. double digits like 9 or 99), and the third substring group will always be english alphabets only.


Solution

  • ^([a-zA-Z]+[0-9]*?[a-zA-Z]+)(\d++)([a-zA-Z]\w*)$
    

    I'm working in a regex playground on my phone so this may work as intended, or not... 😅

    result

    As you can see both Foo99Bar and ARM64x99Bar seem to be caught correctly.