help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: regex: \(.*\)\([a-zA-Z]+\)$ - not working as expected


From: tomas
Subject: Re: regex: \(.*\)\([a-zA-Z]+\)$ - not working as expected
Date: Wed, 25 Mar 2015 13:57:53 +0100
User-agent: Mutt/1.5.21 (2010-09-15)

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Wed, Mar 25, 2015 at 05:27:08AM -0700, AngusC wrote:
> I have text like this:
> 
> some description here Status1
> some other description Status2
> some other interesting description Status1

(I assume the digits at the end of the lines just don't exist:
they disappear later in your post magically anyway ;-)

> I am using regex like this:
> 
> \(.*\)\([a-zA-Z]+\)$

I'll try to read the above aloud:

"First, try to match zero-or-more of anything, call that \1".
"Then try to match at least one character, right before the
end-of-line. Call that \2".

> with replacement text:
> 
> \1 ZZZ \2
> 
> And what I expected was:
> 
> some description here ZZZ Status
> some other description ZZZ Fault
> some other interesting description ZZZ Status

Hm. Just why? It seems you expected the *second* match to
be "as long as possible", but there's no way you told that:

> But instead I got:
> 
> some description here Statu ZZZ s
> some other description Faul ZZZ t
> some other interesting description Statu ZZZ s

Reading aloud the regexp as above makes those matches less
mysterious, doesn't it?

Now: what could you do? Perhaps you might say that you want
the first match to be as short as possible, like so:

  \(.*?\)\([a-zA-Z]+\)$

> I want my expected outcome, how do I do that?

Is that your expected outcome?

> What was wrong with my regex?

As often (it happens to me all the time!), it's just sloppy
specification in your head :-)

Regards
- -- tomás
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)

iEYEARECAAYFAlUSsNEACgkQBcgs9XrR2kbl7ACfXC3spfTmLa7SuVQxf+GXEQSE
iQYAnRr2FrWdjn7S4lGP1ZJhEcMV4Tod
=JuV3
-----END PGP SIGNATURE-----



reply via email to

[Prev in Thread] Current Thread [Next in Thread]