简体   繁体   中英

Simple Regex not working in Perl

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.

Use lookahead instead:

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

Output:

sE^(t * Subscript[r, 1]) t * vE^(t * Subscript[r, 1]) yE^(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.

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