0

I have an issue with something that seems rather trivial but it ain't working, so I'm wondering what I am overlooking.

I have following string :

Just some blablabla with some notes on the end *1 *2

and I need to strip the 'notes' from the string so that it becomes as follows :

Just some blablabla with some notes on the end

My regex is as follows :

regex.Pattern = "^(.*)(\*[0-9]+\s?)+$"

So basically what it should do is look at the end of the string to see if it can find an asterisk followed by one or more integers and an optional space. Unfortunatly if I run this the way I stated it only removes the last note, and leaves the first as below

Just some blablabla with some notes on the end *1

Where is my logic going wrong ?

Thanks in advance!

Wokoman
  • 1,089
  • 2
  • 13
  • 30

2 Answers2

1

The problem on your regex is that you are using a greedy operator:

^(.*)(\*[0-9]+\s?)+$
   ^----- here

Change it to a lazy one (or ungreedy) by adding a ? so it will match the first match of *1

^(.*?)(\*[0-9]+\s?)+$
    ^---- here

Working demo

enter image description here

As an additional note, you could use this regex too:

^(.*?)\*\d
Federico Piazza
  • 30,085
  • 15
  • 87
  • 123
0

You're using (.*), which is greedy. As the engine backtracks, the second group only matches once. Change (.*) to a match-aware construct, see the following regex:

^([^*]+|\*(?!\d))+(\*[0-9]+\s?)+$

Here is a regex demo.

Or you can use a reluctant match from Fede's answer as well, though mine is optimal.

Community
  • 1
  • 1
Unihedron
  • 10,902
  • 13
  • 62
  • 72