如何写这个正则表达式?

时间:2012-04-20 14:48:07

标签: regex

这是包含公式和生物代码的大文件中的示例文章。某些行以下字符开头:

Sheep"-head`ed,   // followed by some normal words 
Mon`o*car*bon"ic,  // followed by some normal words 
mon`o*car"di*an,  // followed by some normal words 
Pol`y*chro"mate,   // followed by some normal words 
sheep"cot`,     // followed by some normal words 
baad,    // followed by some normal words 

我是正则表达式的新手。现在我正在尝试使用TPerlRegEx(a wrapper of PCRE library)。我需要提取:

Sheep"-head`ed,   
Mon`o*car*bon"ic,  
mon`o*car"di*an,  
Pol`y*chro"mate,  
sheep"cot`,    
baad,   

你能帮我写一个正则表达式吗?

非常感谢。

编辑:

感谢大家的帮助。如果他们之间存在正常情况:

Sheep"-head`ed,   // followed by some normal words 
Mon`o*car*bon"ic,  // followed by some normal words 
New test,   //I do not want two or more than two words that end with comma.   
mon`o*car"di*an,  // followed by some normal words 
Pol`y*chro"mate,   // followed by some normal words 
sheep"cot`,     // followed by some normal words 
baad,    // I want this one word that ends with comma

我仍然想要:

Sheep"-head`ed,   
Mon`o*car*bon"ic,  
mon`o*car"di*an,  
Pol`y*chro"mate,  
sheep"cot`,    
baad,   // I want this ONE word that ends with comma.

再次感谢你。

2 个答案:

答案 0 :(得分:3)

原始正则表达式是perl中的^[^,]+,正则表达式:/^[^,]+,/

  • ^匹配行的开头
  • [^ ,]+匹配尽可能多的非逗号,非空格。
  • ,与逗号匹配

答案 1 :(得分:1)

要匹配以给定值开头的行,正则表达式为:

/^startswith/

您必须转义特殊字符。例如:

/^Sheep\"\-head\`ed,/

(我永远无法记住哪些字符需要被转义,但一般来说,即使它不需要它也可以转义任何非字母字符。)

要使一个正则表达式与您的任何示例相匹配,您可以or|一起/^(Sheep\"\-head\`ed,|Mon\`o\*car\*bon\"ic,|...)/ 这样:

{{1}}