1

I have a string that could be formatted in the following ways:

user-style-1
user-style-1-bold
user-style-1-italic
user-style-1-bold-italic

I'm attempting to capture (respectively):

nothing (does not match)
-bold
-italic
-bold and -italic (as separate captures)

This is my RegEx: ^user-style-\d+((-.+?)+?)$ (also potentially ^user-style-\d+(?:(-.+?)+?)$ since I don't care about the full capture, only the individual pieces of it). It captures:

nothing
-bold
-italic
-bold-italic and -italic

or (for the alternate)

nothing
-bold
-italic
-italic

I can't quite figure out how to get the repeating capture group to capture all individual instances rather than the entire thing and just the last instance or just the last instance.

Ben Black
  • 3,751
  • 2
  • 25
  • 43

2 Answers2

3

Take a look at something like this:

^user-style-\d+(-\w+)(-\w+)?$

Regex Demo

You will need a group per pattern that you want to capture. Refer to link posted by ggorlen and the accepted answer for that question for the reasoning.

vs97
  • 5,765
  • 3
  • 28
  • 41
0

Try this RegEx: ^user-style-1(-bold)?(-italic)?$ it should capture all 4 cases

link: https://regex101.com/r/p8NbIv/1

tgralex
  • 794
  • 4
  • 14