[英]Text replace challenge (regex)
I can't solve a problem. 我无法解决问题。 Perhaps it is impossible to achieve what I want.
也许不可能实现我想要的。
GOAL: use only replace function to remove all text except the email address. 目标:仅使用替换功能删除除电子邮件地址外的所有文本。
I have a text with email in: Start text some other text 2828 text my.address@mail.com some additional text.
我的电子邮件中包含以下文本:
Start text some other text 2828 text my.address@mail.com some additional text.
Regular expression to select email: [a-zA-Z0-9\\-\\._]+@[\\w\\d\\-\\._]+\\.\\w{2,12}
选择电子邮件的正则表达式:
[a-zA-Z0-9\\-\\._]+@[\\w\\d\\-\\._]+\\.\\w{2,12}
Regular expression works perfectly to find an email address, but it didn't work to remove all letters from an email. 正则表达式非常适合查找电子邮件地址,但不能删除电子邮件中的所有字母。
Below print screen shows what I got as a result when apply replace function in the text editor: 下面的打印屏幕显示了在文本编辑器中应用替换功能时得到的结果:
As results I used regexp
.*([a-zA-Z0-9\\-\\._]+@[\\w\\d\\-\\._]+\\.\\w{2,12}).*
, and replace it on $1
. 作为结果,我使用了regexp
.*([a-zA-Z0-9\\-\\._]+@[\\w\\d\\-\\._]+\\.\\w{2,12}).*
和将其替换为$1
。 Sadly this workflow give me broken email. 遗憾的是,此工作流程给我发送了破碎的电子邮件。
I used email as an example, the same result I got for any other data types as URLs, IPs, phones, names, cities, zips etc. 我以电子邮件为例,对于其他任何数据类型(如URL,IP,电话,名称,城市,邮政编码等),结果都是相同的。
Can anyone unveil a solution to this problem? 谁能公开解决这个问题的方法? Thank you a lot.
非常感谢。
PS I am not interested in using math() function, because of this function isn't presented in most of the text editors. PS我对使用math()函数不感兴趣,因为大多数文本编辑器都未提供此函数。
I think you should make the first part non greedy .*?
我认为您应该使第一部分不要贪婪
.*?
or else the .*
will match upon the @
and after that just giving up 1 match to satisfy the character class [a-zA-Z0-9\\-\\._]+
否则
.*
将与@
匹配,然后放弃1个匹配项以满足字符类[a-zA-Z0-9\\-\\._]+
If it is not greedy it will capture my.address@mail.com
instead of s@mail.com
如果不是贪婪,它将捕获
my.address@mail.com
而不是s@mail.com
I would do it like this: 我会这样做:
Find: (.*?)[a-zA-Z0-9\\-\\._]+@[\\w\\d\\-\\._]+\\.\\w{2,12}\\s?(.*?)
查找:
(.*?)[a-zA-Z0-9\\-\\._]+@[\\w\\d\\-\\._]+\\.\\w{2,12}\\s?(.*?)
Replace: $1$2
替换:
$1$2
Input: Start text some other text 2828 text my.address@mail.com some additional text
输入:
Start text some other text 2828 text my.address@mail.com some additional text
Output: Start text some other text 2828 text some additional text
输出:
Start text some other text 2828 text some additional text
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.