Simple Regex not working in Perl

I have a simple Perl regular expression that should match the space between two characters and replace the space with *. In some cases, this simply does not work. The Perl line is this:

s/([A-Za-z0-9])\s+([A-Za-z0-9])/\1 * \2/g;

For example, see below: ( ~>- this is my zsh invitation)

~> 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]not mapped. This is just an example. My file is much longer, and when the regex snaps the most correctly, I cannot find a pattern that does not match (and should).

It seems that Vim finds everything correctly (after changing the syntax of the corresponding regular expression).

How can I solve this? How can I help diagnose the problem?

Thanks.

+4
source share
1

lookahead:

perl -pe 's/([a-z0-9])\s+(?=[a-z0-9])/\1 * /ig' mwe

:

s-E ^ (t * [r, 1]) t * v-E ^ (t * [r, 1]) y-E ^ (t * [r, 1]) t * y + E ^ t * s * [r, 1] +2 * E ^ (t * [r, 1]) s * [r, 1] -3 * E ^ (t + t * [r, 1]) s * [r, 1] + E ^ (t * [r, 1]) s * t * [r, 1]

, . , :

perl -pe 's/([a-z0-9])\s+([a-z0-9])/\1 * \2/ig' <<< "a b c"

:

a * b c

b , .

+3

All Articles