Regex for circular replacement
Posted
by polygenelubricants
on Stack Overflow
See other posts from Stack Overflow
or by polygenelubricants
Published on 2010-03-15T07:18:07Z
Indexed on
2010/03/15
7:39 UTC
Read the original article
Hit count: 629
How would you use regex to write functions to do the following:
- Replace lowercase 'a' with uppercase and vice versa
- Where words are separated by whitespaces and
>and<are special markers, replace>wordwithword<and vice versa - Replace postincrement (
i++;) with preincrement (++i;) and vice versa. Variable names are[a-z]+. Input is just a bunch of these statements. Bonus: also do decrement.
Also interested in solutions in other flavors.
Note: this is NOT a homework question. See also my previous explorations of regex:
- Regex split into overlapping strings (Alan Moore's answer is especially instructive)
- Can you use zero-width matching regex in String split? (my solution exploits a known Java regex bug with regards to non-obvious length lookbehind!)
© Stack Overflow or respective owner