简体   繁体   中英

String.replaceAll() with regex gets messed up

I'm trying to implement the Swedish "Robbers language" in Java. It's basically just replacing each consonant with itself, followed by a "o", followed by itself again. I thought I had it working with this code

str.replaceAll("[bcdfghjklmnpqrstvwxz]+", "$0o$0");

but it fails when there are two or more subsequent consonants, for example

String str = "horse";

It should produce hohororsose , but instead I get hohorsorse . I'm guessing the replacement somehow messes up the matching indexes in the original string. How can I make it work?

str.replaceAll("[bcdfghjklmnpqrstvwxz]", "$0o$0");

Remove the + quantifier as it will group consonants.

// when using a greedy quantifier
horse
h   | o | rs    | e
hoh | o | rsors | e

A plus sign matches one or more of the preceding character, class, or subpattern. For example a+ matches ab and aaab. But unlike a* and a?, the pattern a+ does not match at the beginning of strings that lack an "a" character.
https://autohotkey.com/docs/misc/RegEx-QuickRef.htm

+ means: Between one and unlimited times, as many times as possible, giving back as needed (greedy)

+? means: Between one and unlimited times, as few times as possible, expanding as needed (lazy)

{1} means: Exactly 1 time (meaningless quantifier)

In your case you don't need a quantifier.

You can experiment with regular expressions online at https://regex101.com/

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