regex backreference

  • Thread starter Thread starter jg
  • Start date Start date
J

jg

1. backreference
how do I reference and named matched expression in the pattern
suppose I have specified (abc(?<mymatch>,\s+)|(?<mm2>[-/.]))xyz
and if mm2 was matched previously, I want the same matched mm2 char to
be matched after xyz
but if mymtach was matched previously, I want to match space after
space

In order word I want to match only "abc, xyz " , "abc-xyz-","abc/xyz/",
or "abc.xyz."

I must be thick, I tried (?(mm2)<mm2> and my interpretation of
alternation syntax, all end up with syntax error

2. Alternation
I saw the syntax on Microsoft
(?(expression)yes|no)
and
(?(name)yes|no)
How do I make user of yes part?
 
jg said:
1. backreference
how do I reference and named matched expression in the pattern
suppose I have specified (abc(?<mymatch>,\s+)|(?<mm2>[-/.]))xyz
and if mm2 was matched previously, I want the same matched mm2 char to
be matched after xyz
but if mymtach was matched previously, I want to match space after
space

In order word I want to match only "abc, xyz " , "abc-xyz-","abc/xyz/",
or "abc.xyz."

I must be thick, I tried (?(mm2)<mm2> and my interpretation of
alternation syntax, all end up with syntax error

Try this:

abc(( said:
2. Alternation
I saw the syntax on Microsoft
(?(expression)yes|no)
and
(?(name)yes|no)
How do I make user of yes part?

Hm... I thought the explanation on the MS page was really quite good. I
can't readily come up with a good sample where you'd actually need the
syntax, though :-) I'm trying, nevertheless.

The syntax (?(expression)yes|no) makes a zero-width assertion of the
expression and if it matches continues to match the yes expression. If
the assertion didn't match, it continues with the no part instead.
Say you have a line that can start with "date" or it might not.
Depending on that, you want to parse the line differently, saving the
date if there's one. You could do this:

^(?(date)date (?<date>\d\d-\d\d-\d\d\d\d)|.*)$

This would parse out the date if there was one and match the whole line
if the date wasn't found. As I said, this is not a good sample - you can
do it with a different syntax - but I hope it shows the point.

The second syntax (?(name)yes|no) does just the same thing, only it
doesn't evaluate the "name" as an expression, but accesses a named
capture that has already been found instead.


Oliver Sturm
 
thank you again, Oliver. That is wonderful

I had the inkling that the alternation was something like you said but I
must making mistakes trying to use it and thus ended up with syntax error.


I like your choice and like you said I don't see any use for that
?(expression) syntax now for my purpose.

Oliver Sturm said:
jg said:
1. backreference
how do I reference and named matched expression in the pattern
suppose I have specified (abc(?<mymatch>,\s+)|(?<mm2>[-/.]))xyz
and if mm2 was matched previously, I want the same matched mm2 char
to be matched after xyz
but if mymtach was matched previously, I want to match space after
space

In order word I want to match only "abc, xyz " ,
"abc-xyz-","abc/xyz/", or "abc.xyz."

I must be thick, I tried (?(mm2)<mm2> and my interpretation of
alternation syntax, all end up with syntax error

Try this:

abc(( said:
2. Alternation
I saw the syntax on Microsoft
(?(expression)yes|no)
and
(?(name)yes|no)
How do I make user of yes part?

Hm... I thought the explanation on the MS page was really quite good. I
can't readily come up with a good sample where you'd actually need the
syntax, though :-) I'm trying, nevertheless.

The syntax (?(expression)yes|no) makes a zero-width assertion of the
expression and if it matches continues to match the yes expression. If the
assertion didn't match, it continues with the no part instead.
Say you have a line that can start with "date" or it might not. Depending
on that, you want to parse the line differently, saving the date if
there's one. You could do this:

^(?(date)date (?<date>\d\d-\d\d-\d\d\d\d)|.*)$

This would parse out the date if there was one and match the whole line if
the date wasn't found. As I said, this is not a good sample - you can do
it with a different syntax - but I hope it shows the point.

The second syntax (?(name)yes|no) does just the same thing, only it
doesn't evaluate the "name" as an expression, but accesses a named capture
that has already been found instead.


Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
 
Back
Top