代替 。来自电子邮件地址
我有一个文本字符串。其中可能包含也可能不包含电子邮件地址。我想全部更换。 (句号)到点。
blah blah [email protected] fooo some content and email again
吗
blah blah abcd@gmail dot com fooo some content and email again
我可以使用正则表达式执行此操作
? -谢谢 阿伦
I have a text string. That may or maynot contain email addresses. I want to replace all . (fullstop) to dot.
blah blah [email protected] fooo some content and email again
to
blah blah abcd@gmail dot com fooo some content and email again
Can I do this using regex?
-Thanks
Arun
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您可以使用 .NET 正则表达式引擎,则可以通过搜索
(?:.(?=\S+@)|(?<=@\S+).)< 在单个正则表达式中完成此操作/code> 并将所有匹配项替换为
dot
。在 PHP 中,您必须分两步/迭代地完成:
搜索
\.(?=\S+@)
并替换为dot
:这将替换所有电子邮件地址中
@
之前出现的点。然后搜索(@\S+)\.
并替换为\1点
;重复此操作,直到不再有匹配项。像
这样的东西将匹配电子邮件地址中
@
之后的点,但由于 PHP 的正则表达式引擎不支持无限向后查找,我需要将正则表达式重新应用到字符串,次数与最大次数相同@
后的点。例如,在字符串@foo.bar.com
中,它会首先匹配@foo.bar.
并替换为@foo.bar dot
>。然后,在下一次运行中,它将@foo.
替换为@foo dot
。If you had the .NET regex engine at your disposal, you could do it in a single regex by searching for
(?:.(?=\S+@)|(?<=@\S+).)
and replacing all matches withdot
.In PHP, you'd have to do it in two steps/iteratively:
Search for
\.(?=\S+@)
and replace withdot
:This will replace all dots in email addresses that occur before the
@
. Then search for(@\S+)\.
and replace with\1 dot
; repeat this until there are no further matches.Something like
This will match a dot inside an email address after the
@
, but since PHP's regex engine doesn't support infinite lookbehind, I need to reapply the regex to the string as many times as the maximum number of dots after the@
. For example, in the string@foo.bar.com
, it will first match@foo.bar.
and replace with@foo.bar dot
. Then, in the next run, it replaces@foo.
with@foo dot
.第一步 正则表达式从电子邮件地址获取电子邮件句柄 和/或 https://stackoverflow.com/questions/36261/test-expand-my-email-regex
第二步 正则表达式替换“foo-some 空格-bar" 与 "fubar"
采取授人以鱼的方法。
Step one Regex Get Email handle from Email Address and / or https://stackoverflow.com/questions/36261/test-expand-my-email-regex
Step two regex to replace "foo-some white space-bar" with "fubar"
Taking the teach a man to fish approach.