当前位置: 首页 > 知识库问答 >
问题:

方括号后的Regex管

万勇
2023-03-14

我找到了一个我很不明白的正则表达式。

它看起来像这样:

([|)\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b(]|)

我确实知道它试图与255.255等一些数字匹配,并且它应该是一个完整的单词。

但是“([|)”(]|)”是干什么用的呢?最后一个中的方括号和管道的顺序看起来也是错误的。

共有2个答案

章昆琦
2023-03-14

正则表达式的用途尚不清楚。Debugex的可视化效果很好。

调试演示

关于0~255的部分是清楚的(000、00也是可接受的值)。但是尝试匹配|)([]符号有未知的原因。

我认为第一个< code>[和最后一个< code>]出现是因为错误。没有它们,内部正则表达式看起来很合理。但是< code>(|)和< code>\b看起来也不对,所以我的猜测是我们也可以省略< code>(|)。

(|)\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b(|)

调试演示

太叔景同
2023-03-14

Krackmoe,有趣的是,没有([|):这是一种视错觉。

正则表达式引擎看不到([|)

它会看到< code>(,这会打开捕获组1,然后它会看到一个字符类< code>[|)\b(25[0-5],由于几个原因,这个字符类没有多大意义。例如,< code>\b匹配文字字符“b”,字符2和5对于范围< code>0-5是多余的。

所以你不理解它是完全正确的。

我猜作者想在那里加上一个单词边界,但就目前而言,这是一个错别字。

作为参考,这里是正则表达式的逐令牌解释。(别担心,我没有输入所有这些,它是由RegexBuddy自动生成的。)

* Match the regex below and capture its match into backreference number 1 `([|)\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)`
    * Match this alternative (attempting the next alternative only if this one fails) `[|)\b(25[0-5]`
        * Match a single character present in the list below `[|)\b(25[0-5]`
            * A single character from the list “|)” `|)`
            * The character `\b`
            * A single character from the list “(25[” `(25[`
            * A character in the range between “0” and “5” `0-5`
    * Or match this alternative (attempting the next alternative only if this one fails) `2[0-4][0-9]`
        * Match the character “2” literally `2`
        * Match a single character in the range between “0” and “4” `[0-4]`
        * Match a single character in the range between “0” and “9” `[0-9]`
    * Or match this alternative (the entire group fails if this one fails to match) `[01]?[0-9][0-9]?`
        * Match a single character from the list “01” `[01]?`
            * Between zero and one times, as many times as possible, giving back as needed (greedy) `?`
        * Match a single character in the range between “0” and “9” `[0-9]`
        * Match a single character in the range between “0” and “9” `[0-9]?`
            * Between zero and one times, as many times as possible, giving back as needed (greedy) `?`
* Match the character “.” literally `\.`
* Match the regex below and capture its match into backreference number 2 `(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)`
    * Match this alternative (attempting the next alternative only if this one fails) `25[0-5]`
        * Match the character string “25” literally `25`
        * Match a single character in the range between “0” and “5” `[0-5]`
    * Or match this alternative (attempting the next alternative only if this one fails) `2[0-4][0-9]`
        * Match the character “2” literally `2`
        * Match a single character in the range between “0” and “4” `[0-4]`
        * Match a single character in the range between “0” and “9” `[0-9]`
    * Or match this alternative (the entire group fails if this one fails to match) `[01]?[0-9][0-9]?`
        * Match a single character from the list “01” `[01]?`
            * Between zero and one times, as many times as possible, giving back as needed (greedy) `?`
        * Match a single character in the range between “0” and “9” `[0-9]`
        * Match a single character in the range between “0” and “9” `[0-9]?`
            * Between zero and one times, as many times as possible, giving back as needed (greedy) `?`
* Assert position at a word boundary (position preceded or followed—but not both—by a Unicode letter, digit, or underscore) `\b`
* Match the regex below and capture its match into backreference number 3 `(]|)`
    * Match this alternative (attempting the next alternative only if this one fails) `]`
        * Match the character “]” literally `]`
    * Or match this alternative (the entire group fails if this one fails to match)
 类似资料:
  • 以下是降价文本的示例: #“我的标题” !图像标题。{ 样式=“浮动:右; 宽度: 20%; 边框: 1px”} 有的“引用文字”,有的*“强调文字”*等。 在bash脚本中,我试图用法语引号替换任何双引号。 例如:“word”应变成« 换句话说,一个单词前的所有引号都应该替换为一个开放的法语引号,后跟一个不间断的空格;而且一个单词后面的所有引号都要换成一个不换行的空格后面跟着一个闭合的法语引号;

  • 我需要解析一个日志文件并获取时间和相关的函数调用字符串,该字符串存储在日志文件中,如下所示:{“time”:“2012-09-24t03:08:50”,“message”:“call()started”} 在其他字符串字符之间会有多个日志时间函数调用,因此我希望使用regex来遍历文件并获取所有这些 我想获取整个记录的信息,包括花括号 我不断得到非法重复错误,请帮助!谢了。

  • 结果必须是:匹配0=6207匹配1=6204... 希望你能帮助我,谢谢。

  • 当我使用之类的东西时,它仍然会触发,就像连字符无效一样。我尝试了和

  • 在初始化(例如新列表)时使用括号中的括号实际上意味着什么? 这是否意味着创建新的引用方法后正在调用?

  • 我有一个特定的正则表达式,它可以在文本数据中找到一些值,例如任何10个字母,例如。问题是这个值应该只在尖括号、引号或空格内,并且应该提取该值作为结果。例如在这种情况下:

  • 我正在阅读关于if的bash示例,但有些示例是用单方括号编写的: 其他带双方括号: 有什么区别?

  • 我正在尝试找到一种方法,使VSCode在我点击括号内的回车键到括号的开头时自动缩进项目,如下所示(类似于这篇文章)。这在Jupyter中运行良好,但我在VSCode中找不到相同的解决方案,也无法在线找到解决方案。有人知道如何通过VSCode中的预设来实现这一点吗?提前致谢。 当前: 渴望的: 编辑 @Shradha 虽然听起来与我正在寻找的内容相似,但您的建议并没有以我正在寻找的方式解决代码问题。