• 134
    2020-07-06
    "xxx#163.com".replace(/#/,'@')

    作者回复: 赞,这真的是一个特别好的方法。 遇到本文思考题目类似的问题,很多同学可能一上来就想,我要一展身手,想出一个正则,兼容 @ 和 # 两种情况,当然肯定能写出来,也不难。 但提前对文本进行预处理,把杂乱的文本清洗干净,然后再使用正则来提取是一种非常好的思路,通常也是这么做的,比费尽心思想着怎么用正则来兼容各种情况的办法要更好,也更推荐这么做。

    共 2 条评论
    19
  • 吕伟
    2020-07-21
    [a-z,1-9,A-Z]+[#|@][a-z,1-9,A-Z]+.com 一开始是写这样的“\b(\w+)(#|@)(\d+.com)\b” 但是“联系邮箱xxx#163.com”,这样的话就会将中文也混在一起,所以迫于无奈才这样写“[a-z,1-9,A-Z]”

    作者回复: 可以的,中括号里面多个范围不用逗号哈。 也可以考虑看看能不能让 \w 不匹配汉字,比如试试 ASCII模式(如果有的话)

    共 2 条评论
    
  • Robot
    2020-07-06
    public static void main(String[] args) { Pattern pattern = Pattern.compile("[a-zA-Z0-9]+(?:[.-][a-zA-Z0-9]+)*#[a-zA-Z0-9]+(?:[.-][a-zA-Z0-9]+)*\\" + ".[a-zA-Z]+"); String str = "A spirited debate ensued. sd.xxx#gmail.com You may find it entertaining and educational to " + "follow all the various threads. However, I want to focus on one of Mark’s replies. He tweeted about a" + " blog he had yanleichang-3#vip.163.com written back in 2018. I encourage you to read it. You’ll learn" + " something about testing, static typing, and Haskell. You’ll also learn something about how to debate" + " with good-job#qq.com someone in the future by posting your refutation in the think#163.com"; Matcher matcher = pattern.matcher(str); while (matcher.find()) { System.out.println(matcher.group().replace('#','@')); }
    展开

    作者回复: 可以的,其实题目的本意是兼容文本中有 # 和 @ 两种情况

    
    
  • ifelse
    2022-11-21 来自浙江
    学习打卡
    
    
  • 徐能
    2022-06-06
    @Test void replace_email_pound_key_with_at_symbol() { final Pattern pattern = Pattern.compile("(\\W+)(\\w+)(@|#)(\\d+.[a-z]+)(\\W+)"); Matcher match = pattern.matcher("例如网页的底部可能是这样的:联系邮箱:xxx#163.com (请把#换成@)"); assertEquals("例如网页的底部可能是这样的:联系邮箱:xxx@163.com (请把#换成@)", match.replaceAll("$1$2@$4$5")); }
    
    
  • Geek_039a5c
    2022-02-13
    public static void main(String[] args) { String mail = "xxx#163.com"; final Pattern pattern1 = Pattern.compile("\\b([\\w.%+-]+)[#@]([\\w.-]+\\.[a-zA-Z]{2,6})\\b"); Matcher match1 = pattern1.matcher(mail); System.out.println(match1.replaceAll("$1@$2")); }
    
    
  • 取悦
    2021-01-01
    \w+[@#]\w+[.]com 这个对吗
    共 1 条评论
    
  • 小乙哥
    2020-08-27
    pattern = re.compile(r'(\w+)#(\w+)') email_str = 'xxx#163.com' print(pattern.sub(r'\1@\2', email_str))
    
    
  • Juntíng
    2020-08-07
    JavaScriipt: let str = 'xxx#163.com'; 方式1: str.replace(/#/, '@'); 方式2: str.replace(/(\w+)[#@](.\w+)/g, '$1@$2'); 'xxx&163.com,xxx#qq.com'.replace(/(\w+)[#@&](.\w+)/g, '$1@$2');
    
    