正则表达式排除组

时间:2015-12-01 00:16:45

标签: regex regex-group

我有一个简单的字符串word anotherword wordanother AS-00009405 at 01.10.2015 0:00:00,我想要排除所有与我的正则表达式不匹配的数据([A|S]{2}-?[0-9]+)我该怎么办?

2 个答案:

答案 0 :(得分:1)

您没有说出使用哪种语言,但在PHP中您可以这样做:

$word = "word anotherword wordanother AS-00009405 at 01.10.2015 0:00:00";
$pattern = "/.*([A|S]{2}-?[0-9]+).*/";

$word = preg_replace($pattern, "$1", $word);

// $word contains: AS-00009405

通过在开头和结尾添加.*,您必须稍微更改一下正则表达式。

答案 1 :(得分:1)

你可以像下面这样做。

全球查找:(?=[\S\s])(?:(?![AS]{2}-?[0-9]+)[\S\s])*((?:[AS]{2}-?[0-9]+)?)
替换' $1' (我放了一个额外的空格来分隔子串)

Formatted and tested:

 (?= [\S\s] )                         # Assert any character exists ahead
                                      # (this avoids working for nothing)

 (?:                                  # Cluster group 
      (?! [AS]{2} -? [0-9]+ )              # Assert, Not 'my substring'
      [\S\s]                               # Ok, grab any character (advance 1 char position)
 )*                                   # Optional, do 0 to many times

 (                                    # (1 start)
      (?: [AS]{2} -? [0-9]+ )?             # Optional, advance past 'my substring'
 )                                    # (1 end)

示例输入:

word anotherword wordanother AS-00009405 at 01.10.2015 0:00:00, and i want exlude all data that 

word anotherword wordanother AS-00009405 at 01.10.2015 0:00:00, and i want exlude all data that 

word anotherword wordanother AS-00009405 at 01.10.2015 0:00:00, and i want exlude all data that 

输出:

AS-00009405 AS-00009405 AS-00009405