Simple Regex not working in Perl
perl, regex
Solution
Use lookahead instead:
perl -pe 's/([a-z0-9])\s+(?=[a-z0-9])/\1 * /ig' mwe
Output:
s-E^(t * Subscript[r, 1]) t * v-E^(t * Subscript[r, 1]) y-E^(t * Subscript[r, 1]) t * y+E^t * s * Subscript[r, 1]+2 * E^(t * Subscript[r, 1]) s * Subscript[r, 1]-3 * E^(t+t * Subscript[r, 1]) s * Subscript[r, 1]+E^(t * Subscript[r, 1]) s * t * Subscript[r, 1]
Problem is that in your regex you're matching not looking ahead. So for the case of:
perl -pe 's/([a-z0-9])\s+([a-z0-9])/\1 * \2/ig' <<< "a b c"
You will get:
a * b c
Since `b` has already been matched previously and internal pointer has moved ahead.
Problem
I have a simple Perl regex that should match a space between two characters and replace the space with a `*`. It's simply not working in some cases. The Perl line is this: ``` s/([A-Za-z0-9])\s+([A-Za-z0-9])/\1 * \2/g; ``` For example see below: (`~>` is my zsh prompt) ``` ~> cat mwe s t Subscript[r, 1] ~> perl -pe "s/([A-Za-z0-9])\s+([A-Za-z0-9])/\1 * \2/g;" < mwe s * t Subscript[r, 1] ``` `t Subscript[r, 1]` isn't being matched. This is just an example. My file is much longer, and while the regex catches most correctly, I can't find a pattern to the ones it doesn't match (and should). Vim seems to find everything correctly (after the appropriate regex syntax changes). How can I go about solving this? How can I help diagnose the problem? Thank you.