Re: Problem in regexp matching
From: Geoff B (geoff_at_mrc-lmb.cam.ac.uk)
Date: 02/10/04
- Next message: Bruce Hartweg: "Re: Problem in regexp matching"
- Previous message: Helmut Giese: "HTML annotator"
- In reply to: Geoff Battye: "Re: Problem in regexp matching"
- Next in thread: Glenn Jackman: "Re: Problem in regexp matching"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 10 Feb 2004 07:34:58 -0800
Whoops!
Ignore the bit about two quantifiers - I was following Arjen without
double checking - sorry!
If you had got rid of the "?", the rest of my post should be correct!
Of course if the portion you want to extract can include any hex
character rather than just digits, you'll want something like
{0x[0-9a-fA-F]+} instead of {0x\d+}.
Cheers,
Geoff.
Geoff Battye <geoff@mrc-lmb.cam.ac.uk> wrote in message news:<Pine.OSF.4.44.0402101036420.338117-100000@alf1.lmb.internal>...
> On 9 Feb 2004, Senthil wrote:
> > I have out like this
> > 1/1 down 0x12345 down
> > 1/2 down 0x12346 down
> > 1/2 down 0x12347 down
> > I need to grep the pattern 0x12345 from this output.
> > regexp ".*?1/2.*?(\[0-9a-f\]+).*"
> > Whats the mistake in the regexp.
> > Regards
> > Senthil
>
> Some problems with your pattern:
> 1) You're using two quantifiers where you only want one (* and ?)
> 2) You're matching the string "1/2" when not every line has this exact
> string.
> 3) You're pattern \[0-9a-f\]+ is too general, this allows the greedily
> quantified period that follows the 1/2 to match all the way up to just
> before the last letter in your pattern which will match \[0-9a-f\], which
> is the letter d in your second "down".
>
> Remember: your pattern doesn't have to match the whole line, just the part
> you want to extract, and enough else to make sure you are catching the
> right part of your input.
>
> If you are just after the part of each line consisting of 0x followed by
> more digits, then this will do the job:
>
> regexp {0x\d+} $input match
>
> e.g.
> % set input "1/1 down 0x12345 down"
> 1/1 down 0x12345 down
> % regexp {0x\d+} $input match
> 1
> % puts $match
> 0x12345
> %
>
> Regards,
> Geoff.
- Next message: Bruce Hartweg: "Re: Problem in regexp matching"
- Previous message: Helmut Giese: "HTML annotator"
- In reply to: Geoff Battye: "Re: Problem in regexp matching"
- Next in thread: Glenn Jackman: "Re: Problem in regexp matching"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|