I am struggling with a regex to stop at the end of the line.
Input looks like this:
Some lines have values (may contain any characters) after the colons, some don't. There may be spaces either side of the colon, there may not be.
For lines 2 and 4 the following work (i.e. match 12
and 16
respectively):
Pink\s*:\s*(.*)\n
Red\s*:\s*(.*)\n
But for line 3 (where there is no value to match), a regex using the above syntax returns 16, i.e. reads beyond the end of the line.
Can anyone suggest what I'm doing wrong? I'm using VBA.
The problem you have is that \s
shorthand character class matches both vertical and horizontal whitespace. That is, it matches both spaces and newline sequences.
Thus you need to restrict it to match only horizontal whitespace.
You need to replace \s
with [ \t]
or with [^\S\r\n\v]
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With