与内部表达式条件的正则表达
我想构建一个正则表达式,以用“ per”何时替换句子(具有数量的句子的可读版本)。
也就是说:
- “ 3/单位”必须匹配
- “单位/3”必须匹配
- “脚/秒”必须匹配
- “ 05/07”必须匹配
我知道如何创建类似“ \ d+/\ d+”之类的东西。 但是,我该如何构建一条正则说明“不右和左表达式匹配\ d+”?
I would like to build a regular expression for replacing a sentence with "per" when it should be (a readable version of a sentence with quantities).
That is:
- "3/unit" must match
- "unit/3" must match
- "feet/second" must match
- "05/07" must not match
I know how to create something like "\D+/\D+".
But how can I build a regex saying "not both right and left expressions match \D+" ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
regex demo 。 详细信息:
^
- 字符串的开始(?![0-9]+/[0-9]+$)如果有一个或多个数字,
/
,一个或多个数字或字符串位置的结束,这将使匹配失败/
以外的一个或多个字符,/
char,然后是/
You can use
See the regex demo. Details:
^
- start of string(?![0-9]+/[0-9]+$)
- a negative lookahead that fails the match if there are one or more digits,/
, one or more digits and end of string position immediately to the right of the current location[^/]+/[^/]+
- one or more chars other than/
, a/
char, and then one or more chars other than/
$
- end of string.