Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / August 2005

Tip: Looking for answers? Try searching our database.

regex backreference

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
jg - 24 Aug 2005 05:26 GMT
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?
Oliver Sturm - 24 Aug 2005 10:03 GMT
> 1. backreference
>      how do I reference and named matched expression in the pattern
[quoted text clipped - 9 lines]
>     I must be thick, I tried (?(mm2)<mm2>  and  my interpretation of
> alternation syntax, all end up with syntax error

Try this:

 abc((,(?<delim>\s+))|(?<delim>[-/.]))xyz\k<delim>

> 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
Signature

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

jg - 24 Aug 2005 16:19 GMT
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.

>> 1. backreference
>>      how do I reference and named matched expression in the pattern
[quoted text clipped - 43 lines]
>
>                Oliver Sturm

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.