简体   繁体   English

如何找到replace_all的正则表达式?

[英]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 就你而言

  1. you are simply replacing non a-zßäöü with space, 您只是用空间代替非a-zßäöü
  2. and later replacing two or more spaces with one. 然后用一个替换两个或多个空格。

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.

相关问题 如何找到android中正则表达式的所有匹配项 - How can I find all matches to a regular expression in android 如何使用 Java 正则表达式替换 URL 中的所有特殊字符? - How can I replace all special characters in a URL, using a Java regular expression? 如何通过匹配正则表达式提取所有子字符串? - How can I extract all substring by matching a regular expression? 如何找到所有“((”)并将其替换为“(”? - How can I find all “((” and replace them with “(”? java正则表达式查找和替换 - java regular expression find and replace 正则表达式以查找和替换特定的子字符串 - Regular Expression to find & replace specific substring 将Java中所有不在单引号内的“=”替换成正则表达式 - Replace all "=" but not "==" that are not in single quotes with regular expression in Java 如何在 android 中使用正则表达式替换字符串中的某些部分,除了某些单词 - How can i replace some part in string except some word using regular expression in android 如何在JAVA中使用正则表达式在字符串行中找到特定字符串? - how can i find specific strings in string lines using regular expression in JAVA? ANTLR4 如何创建允许除两个选定字符之外的所有字符的正则表达式? - ANTLR4 How can I create a regular expression that allows all characters except two selected ones?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM