【问题标题】:How can I select particular number of character words with regex如何使用正则表达式选择特定数量的字符词
【发布时间】:2013-03-22 11:02:26
【问题描述】:

我有如下文字。

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum 
has been the industry's standard dummy text ever since the fivec harword 1500s, when an unknown printer 
took a galley of type and scrambled it to make a type specimen fivec harword book. It has survived not
only five centuries, but also the leap into electronic typesetting, remaining essentially 
unchanged. It was popularised in the 1960s with the release of fivec harword Letraset sheets containing 
Lorem Ipsum passages, and more recently with desktop publishing software like Aldus 
PageMaker including versions of Lorem Ipsum.

这就是我需要的正则表达式:

1- 选择五个字符。

2- 第一步后选择一个空格。

3- 第二步后选择七个字符。

它应该捕获所有fivec harword 字符串。我该怎么做?

【问题讨论】:

    标签: regex text


    【解决方案1】:

    使用这个:

    \b\w{5}\s\w{7}\b
    

    解释:

    The regular expression:
    
    (?-imsx:\b\w{5}\s\w{7}\b)
    
    matches as follows:
    
    NODE                     EXPLANATION
    ----------------------------------------------------------------------
    (?-imsx:                 group, but do not capture (case-sensitive)
                             (with ^ and $ matching normally) (with . not
                             matching \n) (matching whitespace and #
                             normally):
    ----------------------------------------------------------------------
      \b                       the boundary between a word char (\w) and
                               something that is not a word char
    ----------------------------------------------------------------------
      \w{5}                    word characters (a-z, A-Z, 0-9, _) (5
                               times)
    ----------------------------------------------------------------------
      \s                       whitespace (\n, \r, \t, \f, and " ")
    ----------------------------------------------------------------------
      \w{7}                    word characters (a-z, A-Z, 0-9, _) (7
                               times)
    ----------------------------------------------------------------------
      \b                       the boundary between a word char (\w) and
                               something that is not a word char
    ----------------------------------------------------------------------
    )                        end of grouping
    ----------------------------------------------------------------------
    

    【讨论】:

      【解决方案2】:

      这应该可以解决问题

      (^|\W)\w{5}\s\w{7}($|\W)
      

      (^|\W) 字符串或非单词字符的开头。

      \w{5}一串5个单词的字符

      \s一个空格

      \w{7}一串7个单词的字符

      ($|\W) 字符串结尾或非单词字符

      如果您特别希望字符串周围有空格(而不是标点符号等),请将 \W 替换为 \s

      【讨论】:

      • 这不会在字符串的开头或结尾匹配。
      • 为什么这个不起作用?我在正则表达式的结尾添加了 5 个字符。 \W\w{5}\s\w{7}\s\w{5}\W 我尝试了应该找到的正确文本。
      • M42 是对的,我已经调整了我的以适应字符串的开头和结尾(如果需要,仍然可以灵活地匹配空格)
      【解决方案3】:

      试试这个

      \b[a-zA-Z]{5}\s[][a-zA-Z]{7}\b
      

      \b 表示边界

      [a-zA-Z] 所有 alpha 投注

      {5} 5 个字符,前一个表达式

      \s 单个空格

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-14
        • 2021-11-30
        相关资源
        最近更新 更多