简体   繁体   中英

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.

"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. (How can i say, that it should be 2 or more space? Because + means at least 1, right?)

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,
  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 .

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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