Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 如何编写这个正则表达式?_Regex - Fatal编程技术网

Regex 如何编写这个正则表达式?

Regex 如何编写这个正则表达式?,regex,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

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

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。我需要摘录:

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.
再次感谢您。

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

^匹配行的开头 [^,]+匹配尽可能多的非逗号、非空格。 ,与逗号匹配 原始正则表达式是^[^,]+,perl中的正则表达式:/^[^,]+/

^匹配行的开头 [^,]+匹配尽可能多的非逗号、非空格。 ,与逗号匹配
要匹配以给定值开头的行,正则表达式为:

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

/^Sheep\"\-head\`ed,/
我永远记不清哪些字符需要转义,但一般来说,您可以转义任何非字母字符,即使它不需要转义

要使一个正则表达式与您的任何示例匹配,您可以将它们与|一起使用,如下所示:

/^(Sheep\"\-head\`ed,|Mon\`o\*car\*bon\"ic,|...)/

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

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

/^Sheep\"\-head\`ed,/
我永远记不清哪些字符需要转义,但一般来说,您可以转义任何非字母字符,即使它不需要转义

要使一个正则表达式与您的任何示例匹配,您可以将它们与|一起使用,如下所示:

/^(Sheep\"\-head\`ed,|Mon\`o\*car\*bon\"ic,|...)/

你为什么需要正则表达式?为什么不在/\s+/上拆分每一行?您的示例中的特殊字符实际上是在字符串中,还是在尝试正则表达式语法?@Kip谢谢,它们是真正的示例。为什么需要正则表达式?为什么不在/\s+/上拆分每一行?您的示例中的特殊字符实际上是在字符串中,还是在尝试正则表达式语法?@Kip谢谢,它们是真正的示例。非常感谢。你太快了。如果两行之间有一个正常的两个或两个以上的单词。怎么做?请参见编辑。我想要一个加逗号的单词和一个包含“,*,加逗号的单词。请参见编辑。再次感谢。新的正则表达式中有一个空格,如下所示:/^[^,]+,/非常感谢。你太快了。如果两行之间有一个正常的两个或两个以上的单词。怎么做?请参见编辑。我想要一个加逗号的单词和一个包含“,*,加逗号的单词。请参见编辑。再次感谢您。新的正则表达式中有一个空格,如下所示:/^[^,]+,/谢谢您让我了解有关正则表达式的更多信息。但我不能分开回答。谢谢你们让我了解更多关于正则表达式的知识。但我不能分开回答。