user1472672
user1472672

Reputation: 365

Matching groups in regex

I wanted to match 2 groups using regex from the following Strings:

This is the regex I proposed - Lyra([A-Za-z]{3,6})+(\\d{8}).*

It should match 2 groups. The first one being L4C or CalSWC and the second group the date string which is 8 numbers.

I can get one group to work e.g) LyraL4C(\\d{8}).* but not the one to handle both variations.

Any help much appreciated

Thanks

Upvotes: 0

Views: 38

Answers (2)

Yassin Hajaj
Yassin Hajaj

Reputation: 21995

You might want to use the generic \w selector picking all the words characters to match both versions of your samples

^Lyra(\w{3,6})(\d{8})\.png$

Demo

Escaped for ^Lyra(\\w{3,6})(\\d{8})\\.png$

Upvotes: 2

azro
azro

Reputation: 54168

  • Your pattern Lyra([A-Za-z]{3,6})+(\\d{8}).* is pretty good, but you miss that L4C contains not only letters, but also numbers.
  • Next to that you also specified that the first group has between 3 and 6 elements {3,6} so don't need to use +

So you need Lyra([A-Za-z0-9]{3,6})(\\d{8}).*

See Regex demo

Upvotes: 3

Related Questions