Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regex non capturing groups 1

Status
Not open for further replies.

espar

Programmer
May 13, 2002
66
AU
Hi - any .net regex peeps out there :)

i have this regex to update mapped drives with UNC paths

(d[:|]{1}[\\\/]{1}))(?:[^\\n]\w)

which using expresso successfully gets d:\ or d|\ back with the exception of some d:\n' newline bits here and there.

in .net when using the replace method the non capturing group is captured and those chars are replaced !!

ie: d:\blah\filename.zzz
becomes: \\name\share\ah\filename.zzz

does .net handle non capturing groups?? i cant find great doco on how to do named replacing with .net so am trying simple replace.
 
well i changed it to a negative lookaround and that didnt get included in the capture - whew, itll do what i need.

(d[:|]{1}[\\\/]{1})(?!['\x22\\])
ie. has d:\ or d|\ or d:/ etc but no trailing '"\

but i tried many a type of "non capturing" regex's and .net kept getting all characters (at least for the replace method).

hmmm i will post an answer if i find one :)
 
Capture is different from matching. When you use the Replace method, it is the matched text that gets replaced, not the captured text.

The negative lookahead is the right answer here, as it specifies a matching criterion without actually including it in the match itself.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top