【问题标题】:Regex get text before and after a hyphen正则表达式在连字符前后获取文本
【发布时间】:2020-06-23 00:48:43
【问题描述】:

我有这个字符串:

"Common Waxbill - Estrilda astrild"

如何为连字符前后的单词编写 2 个单独的正则表达式?我想要的输出是:

"Common Waxbill" 

"Estrilda astrild"

【问题讨论】:

  • 为什么不用连字符作为分隔符分割字符串呢?不需要正则表达式。
  • 你想完成什么?

标签: regex regex-lookarounds


【解决方案1】:

这很简单:

.*(?= - )     # matches everything before " - "
(?<= - ).*    # matches everything after " - "

lookaround assertions 上查看本教程。

【讨论】:

  • 当我在收到此错误后使用代码匹配所有内容时:SyntaxError: Invalid regular expression: /(?
  • Javascript 没有lookbehinds。
  • 自 ECMAScript 2018 起,该限制已被取消。 JS 现在支持后向断言,甚至是那些不定长度的断言。
【解决方案2】:

如果你不能使用look-behinds,但你的字符串总是采用相同的格式并且不能包含多个连字符,你可以使用

^[^-]*[^ -] 用于第一个,\w[^-]*$ 用于第二个(或[^ -][^-]*$,如果连字符后的第一个非空格不一定是单词字符。

一点解释: ^[^-]*[^ -] 匹配字符串的开头(锚点^),后跟任意数量的字符,不是连字符,最后是不是连字符或空格的字符(只是为了从匹配中排除最后一个空格)。

[^ -][^-]*$ 采用相同的方法,但反过来,首先匹配一个既不是空格也不是连字符的字符,然后是任意数量的字符,不是连字符,最后是字符串的结尾(锚$ )。 \w[^-]*$ 基本相同,它使用更严格的\w 而不是[^ -]。这再次用于从匹配中排除连字符后的空格。

【讨论】:

  • 这对我有用。据我所知,您能解释一下 [^ -][^-] 的工作原理吗?
  • 很好奇它是如何工作的,因为第一个有一个(轻微的)错误。添加了一些解释。
【解决方案3】:

另一种解决方案是在连字符上拆分字符串并删除空格。

【讨论】:

    【解决方案4】:

    两种替代方法

    您的问题的主要挑战是您需要两个单独的项目。这意味着您的流程依赖于另一种语言。 RegEx 本身不解析或分离字符串;它只解释了我们正在寻找的内容。您使用的语言将进行实际分离。我的答案在 PHP 中得到了你的结果,但其他语言应该有类似的解决方案。

    如果您只想完成问题中的工作,如果您使用的是 PHP...

    方法一:explode("-", $list); -> $array[]

    如果您的列表超过两个项目,这很有用:

    <?php
    // Generate our list
    $list = "Common Waxbill - Estrilda astrild";
    $item_arr = explode("-", $list);
    
    // Iterate each
    foreach($item_arr as $item) {
      echo $item.'<br>';
    }
    
    // See what we have
    echo '
    <pre>Access array directly:</pre>'.
    '<pre>'.$item_arr[0].'x <--notice the trailing space</pre>'.
    '<pre>'.$item_arr[1].' <--notice the preceding space</pre>';
    

    ...您可以使用trim() 清理每个项目并将它们重新分配给一个新数组。这将得到您的问题所要求的文本(之前或之后没有额外的空格)......

    // Create a workable array
    $i=0; // Start our array key counter
    foreach($item_arr as $item) {
      $clean_arr[$i++] = trim($item);
    }
    
    // See what we have
    echo '
    <pre>Access after cleaning:</pre>'.
    '<pre>'.$clean_arr[0].'x <--no space</pre>'.
    '<pre>'.$clean_arr[1].' <--no space</pre>';
    ?>
    

    输出:

    Common Waxbill
    
    Estrilda astrild
    
    Access array directly:
    
    Common Waxbill x <--notice the trailing space
    
     Estrilda astrild <--notice the preceding space
    
    Access after cleaning:
    
    Common Waxbillx <--no space
    
    Estrilda astrild <--no space
    

    方法二:substr(strrpos()) & substr(strpos())

    如果您的列表只有两个项目,这很有用:

    <?php
    // Generate our list
    $list = "Common Waxbill - Estrilda astrild";
    
    // Start splitting
    $first_item = trim(substr($list, strrpos($list, '-') + 1));
    $second_item = trim(substr($list, 0, strpos($list, '-')));
    
    // See what we have
    echo "<pre>substr():</pre>
    <pre>$first_item</pre>
    <pre>$second_item</pre>
    ";
    ?>
    

    输出:

    substr():
    
    Estrilda astrild
    
    Common Waxbill
    

    注意strrpos()strpos() 是不同的,每个都有不同的语法。

    如果您不使用 PHP,但您想使用其他语言而不依赖于 RegEx,那么了解该语言会很有帮助。

    一般来说,编程语言都带有用于此类工作的开箱即用工具,这也是人们选择他们所从事的语言的部分原因。

    【讨论】:

      猜你喜欢
      • 2021-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-04
      • 1970-01-01
      • 2014-05-04
      相关资源
      最近更新 更多