正则表达式匹配组排列

问题描述:

我想标志匹配下不同的排列顺序串满足这些标准正则表达式匹配组排列

1)两个英文字母“S”,“T”应该出现一次且仅一次

2 )字母'n','o','p'其中任何一个都可以出现零次或一次

有没有可以满足这个目的的正则表达式?

+1

你的意思是字母来代替字母? – LeonardChallis 2013-02-15 11:37:31

+1

我不愿意提交元提案以禁止只添加正则表达式标签的问题。 – 2013-02-15 11:38:29

+0

预览可能会对您有所帮助:http://www.regular-expressions.info/lookaround.html。 – 2013-02-15 11:39:37

你可以使用这个表达式

^(?=[^s]*s[^s]*$)(?=[^t]*t[^t]*$)(?=[^n]*n?[^n]*$)(?=[^o]*o?[^o]*$)(?=[^p]*p?[^p]*$).*$ 
    ----------------    -----------------     
     |         |->matches further only if there is 0 or 1 occurance of n 
     | 
     |->matches further only if there is a single occurance of s 

使用singlelinedotall选项与正则表达式

+0

我不认为你最后需要'。* $'。 – 2013-02-15 11:43:29

+0

@FelixKling是的确的..你是正确的..如果他只想检查匹配,那么没有必要.. – Anirudha 2013-02-15 11:47:35

+0

我不觉得这与grep一起使用。 echo“nsot”| grep'^(?= [^ s] * s [^ s] * $)(?= [^ t] * t [^ t] * $)(?= [^ n] * [^ n] * $)(?= [^ o] * o?[^ o] * $)(?= [^ p] * p?[^ p] * $)。* $' this不匹配。我在这里错过了什么? – Shankar 2013-02-15 11:49:57