Regex: does not match end of line

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

The input is as follows:

How text spacing looks in Notepad ++

Some lines have values โ€‹โ€‹(may contain any characters) after the colon, and some do not. There may be gaps on both sides of the colon, and maybe not.

For lines 2 and 4, the following work (i.e. corresponds to 12 and 16 respectively):

 Pink\s*:\s*(.*)\n Red\s*:\s*(.*)\n 

But for line 3 (where there is no value for matching), a regular expression using the above syntax returns 16, that is, it reads outside the line.

Can anyone suggest what I'm doing wrong? I am using VBA.

+5
source share
1 answer

The problem is that the abbreviated \s class matches both vertical and horizontal spaces. That is, it matches both spaces and newline sequences.

Thus, you need to limit it to matching only horizontal spaces.

You need to replace \s with [ \t] or [^\S\r\n\v] .

+3
source

Source: https://habr.com/ru/post/1243325/


All Articles