如何从正则表达式搜索中排除某个单词?
如何为以下问题创建正则表达式:
我有一个字符串,name1=value1;name2=value2;.....;
在某个地方,存在一对, “开始=10072011;”
我需要使用正则表达式从字符串中解析所有 name=value;对,其中值为数字。但是,我想忽略名称 begin
目前我有以下正则表达式:
([\\w]+)=([\\d]+);
我选择 begin
名称。如何将其更改为不包含 begin
?
How I can create a regular expression for the following problem:
I have a string,name1=value1;name2=value2;.....;
Somewhere, there exists a pair,"begin=10072011;"
I need, with regular expressions, to parse from the string all name=value; pairs, where the value is a number. However, I want to ignore the name begin
Currently I have the following regexp:
([\\w]+)=([\\d]+);
Mine selects the begin
name. How can I change it to not include begin
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
(?!begin)\b(\w+)=(\d+);
这使用负向前查找,因此如果字符串以“begin”开头,则不会匹配。
\b
是必需的,以便正则表达式不只是跳过“b”并匹配“egin=...”。请注意,在描述正则表达式时,您应该只使用单个反斜杠进行转义,尽管对于某些语言,您将需要使用双反斜杠来转义反斜杠。
(?!begin)\b(\w+)=(\d+);
This uses negative lookahead, so it will not match if the string starts with "begin". The
\b
is necessary so that the regex does not just skip the "b" and match "egin=...".Note that when describing a regex you should only using a single backslash for escapes, although for some languages you will need to use double backslashes to escape the backslash.
应该这样做:
作为 C++ 字符串文字,它看起来像这样:
\b
是 字边界;您使用它来确保匹配整个单词(因为“单词”是在正则表达式的上下文中定义的;请仔细阅读该页面)。例如,如果没有第一个\b
,正则表达式将正确地无法匹配......但随后它会向前跳过一个位置并匹配:
This should do it:
As aC++ string literal it would look like this:
\b
is a word boundary; you use it to make sure you're matching a whole word (as "word" is defined in the context of regexes; read that page carefully). For example, without the first\b
the regex would correctly fail to match...but then it would skip ahead one position and match:
我认为
(?<=begin=)\d+(?=;)
将是更好的选择。如果将所有信息都保留为 XML 格式,那么工作将会比现在容易得多。
I think
(?<=begin=)\d+(?=;)
will be a better choice.If you keep all the information in XML format, the work will be much easier than now.