[英]How can I find this regular expression for replace_all?
I have tried a lot but coul'nd find a way to combine these two regular expressions in replace_all. 我已经尝试了很多,但是无法找到在replace_all中组合这两个正则表达式的方法。
"test String".replaceAll("(?i)[^a-zßäöü]", " ").replaceAll(" +", " "));
The first regex deletes every symbol, that is not part of the german and the second regex deletes every space combination 2 or more. 第一个正则表达式删除不属于德语的每个符号,第二个正则表达式删除每个等于或大于2的空格。 (How can i say, that it should be 2 or more space? Because + means at least 1, right?)
(我怎么说,它应该是2个或更多的空间?因为+表示至少1,对吗?)
Thanks :) 谢谢 :)
In some cases combining your replaces is not that easy since first replace may produce some result which should be included in second replace. 在某些情况下,组合您的替换并非易事,因为第一次替换可能会产生一些结果,应将其包括在第二次替换中。
In your case 就你而言
a-zßäöü
with space, a-zßäöü
, In other words if you have data like "ab.,!@#cd ef
it will be transformed at first into ab cd ef
and later into ab cd ef
. 换句话说,如果您有
"ab.,!@#cd ef
,它将首先转换为ab cd ef
然后再转换为ab cd ef
。
In other words you are replacing set of one or more characters which are not the ones you are accepting with one space. 换句话说,您要用一个空格替换不是一个或多个字符的一组字符。 So you probably should be fine with simple
所以您可能应该简单就可以了
replaceAll("(?i)[^a-zßäöü]+", " ") // space is also included in [^a-zßäöü]
// but that should be fine since replacing
// space with space shouldn't break anything
// (especially if it worked in your original solution)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.