unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Yuri Khan <yuri.v.khan@gmail.com>
To: AngusC <anguscomber@gmail.com>
Cc: "help-gnu-emacs@gnu.org" <Help-gnu-emacs@gnu.org>
Subject: Re: regex: \(.*\)\([a-zA-Z]+\)$ - not working as expected
Date: Wed, 25 Mar 2015 18:54:24 +0600	[thread overview]
Message-ID: <CAP_d_8VZwdVzWBhECpu5ScNBpWah5KFK+nhWDW1rkZrKbgSbKw@mail.gmail.com> (raw)
In-Reply-To: <1427286428315-353090.post@n5.nabble.com>

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.)



  parent reply	other threads:[~2015-03-25 12:54 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-03-25 12:27 regex: \(.*\)\([a-zA-Z]+\)$ - not working as expected AngusC
2015-03-25 12:50 ` K.P. Huang
2015-03-25 12:54 ` Yuri Khan [this message]
2015-03-25 14:16   ` AngusC
2015-03-25 12:57 ` tomas
     [not found] <mailman.2738.1427286469.31049.help-gnu-emacs@gnu.org>
2015-03-25 12:43 ` Loris Bennett
2015-03-25 12:45   ` Loris Bennett

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAP_d_8VZwdVzWBhECpu5ScNBpWah5KFK+nhWDW1rkZrKbgSbKw@mail.gmail.com \
    --to=yuri.v.khan@gmail.com \
    --cc=Help-gnu-emacs@gnu.org \
    --cc=anguscomber@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).