为什么电子邮件regex给人一种error的invalid regular expression '^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$', reason 'Invalid character range'
blogs.smpl <- "mail:mami@yahoo.com: subject:Lorem Ipsum body: is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s"
blogs.smpl <- gsub("^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+$","",blogs.smpl)
Run Code Online (Sandbox Code Playgroud)
因为-应该只在字符类的开头或结尾。否则,它意味着它之前和之后的符号之间的范围。
最后一个字符类有问题:[a-zA-Z0-9-.]. 它必须转向[a-zA-Z0-9.-]。
注意:在 R 中,除非您使用perl=TRUE.
此外,请参阅R 字符串操作PDF,了解有关 R 字符类(第 2 页)和一般正则表达式的更多信息。这是摘录:
以下是关于如何在字符类中将字符匹配为常规字符的一组规则: 要
]在字符类中匹配,请将其放在首位。要
-在字符类中匹配,请将其放在首位或最后。要
^在字符类内部匹配,请将其放在任何地方,但首先要。要匹配
\字符类中的任何其他字符或元字符(但),请将其放在任何位置。