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):
对于第2和第4行,以下工作(即分别匹配12和16):
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.
但对于第3行(没有值匹配),使用上述语法的正则表达式返回16,即超出行尾的读取。
Can anyone suggest what I'm doing wrong? I'm using VBA.
任何人都可以建议我做错了什么?我正在使用VBA。
1 个解决方案
#1
3
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.
你遇到的问题是\ n的速记字符类匹配垂直和水平空格。也就是说,它匹配空格和换行序列。
Thus you need to restrict it to match only horizontal whitespace.
因此,您需要将其限制为仅匹配水平空格。
You need to replace \s
with [ \t]
or with [^\S\r\n\v]
.
您需要用[\ t]或[^ \ S \ r \ n \ v]替换\ s。
#1
3
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.
你遇到的问题是\ n的速记字符类匹配垂直和水平空格。也就是说,它匹配空格和换行序列。
Thus you need to restrict it to match only horizontal whitespace.
因此,您需要将其限制为仅匹配水平空格。
You need to replace \s
with [ \t]
or with [^\S\r\n\v]
.
您需要用[\ t]或[^ \ S \ r \ n \ v]替换\ s。