热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

正则表达式:未能匹配行尾-Regex:failingtomatchendofline

Iamstrugglingwitharegextostopattheendoftheline.我正在努力与正则表达式停止在行尾。Inputlookslikethi

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):

对于第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。


推荐阅读
author-avatar
文人博客
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有