简体   繁体   English

正则表达式用问号替换逗号分隔的单词

[英]Regular Expression to replace comma separated words with question marks

I want to replace the following strings:我想替换以下字符串:

chrome, internet explorer, mozlla, opera铬, 互联网浏览器, mozlla, 歌剧

or或者

john, jack, jill, bill约翰、杰克、吉尔、比尔

with

?, ?, ?, ? ?, ?, ?, ?

I need a regular expression for Java which can replace the above string.我需要一个 Java 的正则表达式,它可以替换上面的字符串。 Find all words and replace with question marks查找所有单词并用问号替换

Something like this:像这样的东西:

String output = myString.replaceAll("(chrome|internet|explorer|mozlla|opera)", "?");

[Edit] You are changing the question faster then I can answer. [编辑]您正在更快地更改问题,然后我可以回答。

To replace any word:要替换任何单词:

String output = myString.replaceAll("\b(\w+)\b", "?");

\b - first or last character in the word \b - 单词中的第一个或最后一个字符
\w - alphanumeric \w - 字母数字
+ - one or more repetitions + - 一次或多次重复

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM