Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex: failing to match end of line

Tags:

regex

vba

I am struggling with a regex to stop at the end of the line.

Input looks like this:

How text spacing looks in Notepad++

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.

like image 364
QAer Avatar asked Oct 19 '22 15:10

QAer


1 Answers

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].

like image 166
Wiktor Stribiżew Avatar answered Oct 21 '22 23:10

Wiktor Stribiżew