字边界?或者是其他东西 ? [英] Word boundary ? or something else ?

查看:111
本文介绍了字边界?或者是其他东西 ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我尝试构建模式以匹配"sign-value-unit"字符串,这些模式必须是动态的,根据文化。正文和负号以及小数分隔符在文化中指定,我们定义单位是"所有不是数字或正号或负号或小数分隔符(或cr和lf)"

I'm try to build patterns to match strings that are "sign-value-unit", these patterns must be dynamic according to a Culture. The positive and negative signs and the decimal separator are specified in the culture, and we defined that the unit is "everything that is not a digit or the positive or negative sign or decimal seperator (or cr and lf)"

当文化为正号和负号分别指定为"+"和" - "而小数分隔符为"。"时,我的最终模式如下所示:
(?< sign> [ \ + - ])?\?(?< value>(?:\d +(\。\\\ *)?)|(?:\。\ d +))\?(? < unit> [^ - \ + \.\r\\\
\0x00] +)?

如您所见,每个部分之间允许有一个可选空间。此模式匹配并捕获以下字符串:" + 1.2unit"或"+1.2单位"

When the culture specify as '+' and '-' for positive and negative signs and '.' for decimal separator my final pattern looks like this:
(?<sign>[\+-])?\ ?(?<value>(?:\d+(\.\d*)?)|(?:\.\d+))\ ?(?<unit>[^-\+\.\r\n\0x00]+)?

As you can see, an optional space is allowed between each part.
This pattern matches and capture the following strings : "+1.2unit" or "+1.2 unit"

当符号和/或小数分隔符是单词而不是字符时,会出现问题。让我们拥有"加号"和"减"作为标志和"点"作为辞职者。我的模式的单位部分现在看起来如下:

(?< unit> \b (?:(?!\d |(?:Plus) )|(?:减号)|(?:点)| \ r | \ n | \0x00)。)*)?

My problem occurs when the signs and/or decimal separator are words instead of characters. Let's ay that I have "Plus" and "Minus" as signs and "Point" as serator. The unit part of my pattern that now looks like this:

(?<unit>\b (?:(?!\d|(?:Plus)|(?:Minus)|(?:Point)|\r|\n|\0x00).)*)?

如果我不使用单词边界锚(粗体),我的单位模式捕获" oint "在" 1Point2单位"中这不是我的期望。
如果我使用单词boundary,它适用于" 1Point2 Unit "但如果价值和单位之间没有空格,则无法捕获单位:" 1Point2Unit "

If I do not use the word boundary anchor (in bold), my unit pattern captures "oint " in "1Point2 Unit " that is not what I expect.
If I use the word boundary, it works fine for "1Point2 Unit " but fails to capture the unit if there is no space between value and unit : "1Point2Unit "

我认为我承认为什么我的模式未能捕获,但我不知道如何修改它,成功。我是完全错误的,试图通过使用\b来做到这一点。有人可以帮助我吗?

乔乔

I think that I undertand why this my pattern fails to capture, but I cleary don't know how I can modify it, to succeed. My be I'm totally wrong trying to do this by using \b.
Could someone, please, help me ?


Jojo

推荐答案

查看正则表达式如果条件。这样你可以做一个 prememtive 查看数据,如果它是文本,做一件事,否则做其他事情。我在我的博客上讨论它:

正则表达式和If条件

请注意我在博客上的示例中我匹配if处理中的不同组名。没有理由不在if的不同部分找到类似的匹配组名称。

HTH
Check out the regex if conditonal . That way you can do a prememtive look at the data, if it is text, do one thing, otherwise do something else. I discuss it on my blog:

Regular Expressions and the If Conditional

Note in my example on the blog I match to different group names in the if processing. There is no reason not to have similar match group names though found in different parts of the if.

HTH


这篇关于字边界?或者是其他东西 ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆