[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: regex: \(.*\)\([a-zA-Z]+\)$ - not working as expected
From: |
Yuri Khan |
Subject: |
Re: regex: \(.*\)\([a-zA-Z]+\)$ - not working as expected |
Date: |
Wed, 25 Mar 2015 18:54:24 +0600 |
On Wed, Mar 25, 2015 at 6:27 PM, AngusC <anguscomber@gmail.com> wrote:
> I have text like this:
>
> some description here Status1
>
> I am using regex like this:
>
> \(.*\)\([a-zA-Z]+\)$
>
> with replacement text:
>
> \1 ZZZ \2
>
> And what I expected was:
>
> some description here ZZZ Status
>
> But instead I got:
>
> some description here Statu ZZZ s
Your regex consists of two parts. The first part matches any sequence
of characters. The second part matches any non-empty sequence of basic
Latin letters. There are many ways to match your text with this regex.
Most regular expression engines use the maximum munch rule — i.e. the
first part tries to match as much of the string as possible while
still satisfying the regex.
Others have suggested adding a space to disambiguate the match.
Alternatively, you can make the first group non-greedy so that instead
of matching as much as possible it will match as little as possible:
\(.*?\)\([a-zA-Z]+\)$
(Note the question mark in the first group.)