2

这是一个包含公式和生物代码的巨大文件的样本。有些行下列字符开头:

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(PCRE 库的包装器)。我需要提取:

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.

再次感谢你。

4

2 回答 2

3

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

  • ^ 匹配行首
  • [^ ,]+匹配尽可能多的非逗号、非空格。
  • , 匹配逗号
于 2012-04-20T14:50:53.397 回答
1

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

/^startswith/

您将不得不转义特殊字符。例如:

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

(我永远记不清哪些字符需要转义,但通常你可以转义任何非字母字符,即使它不需要它。)

对于一个匹配您的任何示例的正则表达式,您可以像这样将or它们一起使用|

/^(Sheep\"\-head\`ed,|Mon\`o\*car\*bon\"ic,|...)/
于 2012-04-20T14:51:13.140 回答