正则表达式与换行符匹配

时间:2010-10-11 03:10:46

标签: regex

如何匹配可在任何地方发生换行的正则表达式?

例如,如果我想要匹配“数以千计的海龟蛋”,它应该符合以下所有情况。 (甚至是换行符在单词内的情况。)

Scientists have revealed that a mammoth effort to move *thousands of turtle eggs* from beaches around the Gulf of Mexico after the Deepwater Horizon oil spill may have saved almost 15,000 of the reptiles.   

Scientists have revealed that a mammoth effort to move *thousands 
of turtle eggs* from beaches around the Gulf of Mexico after the Deepwater Horizon oil spill may have saved almost 15,000 of the reptiles.

Scientists have revealed that a mammoth effort to move *thousands of 
turtle eggs* from beaches around the Gulf of Mexico after the Deepwater Horizon oil spill may have saved almost 15,000 of the reptiles.

Scientists have revealed that a mammoth effort to move *thousands of turtle 
eggs* from beaches around the Gulf of Mexico after the Deepwater Horizon oil spill may have saved almost 15,000 of the reptiles.

3 个答案:

答案 0 :(得分:3)

  /thousands\s+of\s+turtle\s+eggs/

或此版本确保数千鸡蛋不属于另一个词(如 ... eggsbath

  /\bthousands\s+of\s+turtle\s+eggs\b/

答案 1 :(得分:0)

您可以使用标记“s”来匹配所有换行符。

如果您使用正则表达式“/reptiles..*?sc/gis”,它将匹配“reptiles.sc”

您可以尝试此link

这是一个在线正则表达式编辑器

答案 2 :(得分:0)

使用's'开关。然后^ $成为整个字符串的开始和结束,换行符被视为空格。所以只要你使用\ s来匹配单词之间的差距。 例如:

  

#thousands \ SOF \ sturtle \ seggs#SI