英文:
How can I regex without certain suffices?
问题
https://regex101.com/ <- 供那些想要测试正则表达式的人使用。
我正在开发一个印尼价格解析器。
假设我有以下示例:
- 150 k
- 150 kilobyte
- 150 ka
- 150 k2
- 150 k)
- 150 k.
我们知道1)、5)、6)可以是价格,而其他的显然不是。
我的正则表达式实际上比较复杂,但为了简单起见,
假设我的正则表达式是:[0-9]+(\s*[k])
这可以匹配1)到6)中的所有内容。
所以我在正则表达式中加入了[^0-9a-zA-Z]:[0-9]+(\s*[k])[^0-9a-zA-Z]
现在我只得到了1)、5)、6),这是可以的。
然而,问题是...它们有不必要的后缀,比如[ ) , ]
我该如何解析出只有'150 k'这样的内容,而没有与价格信息无关的后缀呢?
在获取5)、6)后,我是否需要再进行一次处理,手动去掉这些后缀?
提前感谢任何想法。
英文:
https://regex101.com/ <- for those who want to test regex.
I'm working on Indonesian price parser.<br><br>
Say, I have below examples:<br><br>
- 150 k<br>
- 150 kilobyte<br>
- 150 ka<br>
- 150 k2<br>
- 150 k)<br>
- 150 k.<br><br>
We know 1), 5), 6) can be the price, while remains obviously cannot be.<br>
My regex is bit complicated in real, but for simplicity,<br><br>
Let's say my regex is: [0-9]+(\s*[k])<br><br>
This catches 1) to 6), all of them.<br><br>
So I put [^0-9a-zA-Z] to regex: [0-9]+(\s*[k])[^0-9a-zA-Z]<br><br>
Now I got 1), 5), 6) only, and this is fine.<br><br>However, the problem is... they have unnecessary suffix like [ ) , ]<br><br>
How can I parse just '150 k' without any suffix like [ ) , ] which is not related to price information?<br><br>
Should I have one more process after get 5), 6) manually getting rid of those suffices?
Thank you in advance to any idea.
答案1
得分: 2
你可以使用一个单词边界 - \b
。你也可以在开头使用它,而不是空格:
\b[0-9]+\s*k\b
工作示例:https://regex101.com/r/RAF2Vg/3
英文:
You can use a word boundary - \b
. You can also use one at the start, instead of the space:
\b[0-9]+\s*k\b
Working example: https://regex101.com/r/RAF2Vg/3
答案2
得分: 2
我认为(\d+\s*k)\b
会满足你的需求。它将检查在"k"之后是否达到了一个词边界。这个词边界可以是任何东西,是的,甚至可以是一个")"。请参考这个示例。
英文:
I think (\d+\s*k)\b
will serve your purpose. It will check if after the 'k' a word boundary has been reached. This word boundary can be anything, yes, even a )
. Look at this example
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论