与内部表达式条件的正则表达

发布于 2025-02-11 16:36:44 字数 222 浏览 1 评论 0原文

我想构建一个正则表达式,以用“ 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜吻♂芭芘 2025-02-18 16:36:44

您可以使用

^(?![0-9]+/[0-9]+$)[^/]+/[^/]+$

regex demo 详细信息

  • ^ - 字符串的开始
  • (?![0-9]+/[0-9]+$)如果有一个或多个数字,/,一个或多个数字或字符串位置的结束,这将使匹配失败
  • 。 ]+ - /以外的一个或多个字符,/ char,然后是/
  • < chars或多个字符代码> $ - 字符串结尾。

You can use

^(?![0-9]+/[0-9]+$)[^/]+/[^/]+$

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文