Regular Expression to Capture VB Declarations in Visual Studios Find/Replace

  • Thread starter Thread starter JackRazz
  • Start date Start date
J

JackRazz

I'm trying to use Visual Studio's Find/Replace to match VB declarations. This RegEx
works fine in Regulator:


^\s*(Public|Friend|Protected|Private)*\s*(Shared|Overrides)*\s*(Sub|Function|Property
)+

But when I try it in Visual Studio's find (with regular expressions turned on) it
doesn't work. Are there any known problems with VS find or is there something wrong
with the above expression? I'm new to them, so the second question could easily be
the case.

Also, Ive seen example programs that use RegEx to parse code to format as html, but I
can't remember where it was. Anyone remember this?

Thanks in Advance - JackRazz
 
The regex engine in the Find... dialog isn't the one provided by the .net
framework. Click the 'Help' button to get a list of regex expressions valid
in the find/replace dialogs.
I think the main difference for you regex is that spaces are matched by ":b"
in the find dialog.

JackRazz said:
I'm trying to use Visual Studio's Find/Replace to match VB declarations. This RegEx
works fine in Regulator:


^\s*(Public|Friend|Protected|Private)*\s*(Shared|Overrides)*\s*(Sub|Function
|Property
)+

You know this matches "FriendFriendFriendOverridesSharedSubSubSub", too?
Maybe try something like
:b*(|Public|Friend|Protected|Private):b+(|Shared|Overrides):b+(Sub|Function|
Property)

Niki
 
Niki,
That was it! Thanks. I think I'm finally getting these regular expressions. This
is my new expression:
(Public|Friend|Protected|Private):Zs*(Shared|Overrides|.):Zs*(Sub|Function|Property):
Zs+

One question, I'm having problems with the middle section for the optional Shared or
Overides keyword. It seems to me that this should work, but doesn't:
(Shared|Overrides):Zs*

If I add the last '|.', it works
(Shared|Overrides|.):Zs*

Any ideas why I need the '|.' as the * at the end of the middle section should make
it optional ie. 0 is ok

Thanks - JackRazz



| The regex engine in the Find... dialog isn't the one provided by the .net
| framework. Click the 'Help' button to get a list of regex expressions valid
| in the find/replace dialogs.
| I think the main difference for you regex is that spaces are matched by ":b"
| in the find dialog.
|
| | > I'm trying to use Visual Studio's Find/Replace to match VB declarations.
| This RegEx
| > works fine in Regulator:
| >
| >
| >
| ^\s*(Public|Friend|Protected|Private)*\s*(Shared|Overrides)*\s*(Sub|Function
| |Property
| > )+
|
| You know this matches "FriendFriendFriendOverridesSharedSubSubSub", too?
| Maybe try something like
| :b*(|Public|Friend|Protected|Private):b+(|Shared|Overrides):b+(Sub|Function|
| Property)
|
| Niki
|
|
 
JackRazz said:
Niki,
That was it! Thanks. I think I'm finally getting these regular expressions. This
is my new expression:
(Public|Friend|Protected|Private):Zs*(Shared|Overrides|.):Zs*(Sub|Function|P
roperty):
Zs+

One question, I'm having problems with the middle section for the optional Shared or
Overides keyword. It seems to me that this should work, but doesn't:
(Shared|Overrides):Zs*

The '*' quantifier only applies to one character. You can make it apply to a
larger pattern by using baces:
((Shared|Overrides):Zs*)*
Read: "Shared or Overrides, followed by any number of spaces, the whole
repeated any number of times"
If I add the last '|.', it works
(Shared|Overrides|.):Zs*

Any ideas why I need the '|.' as the * at the end of the middle section should make
it optional ie. 0 is ok

See above.
BTW: You don't need "|.", but only "|" (i.e. (a|b|c|) makes the whole thing
optional).

Niki
 
Thanks Niki,
I thought it applied to the whole thing. Appreciate your help.

JackRazz



| The '*' quantifier only applies to one character. You can make it apply to a
| larger pattern by using baces:
| ((Shared|Overrides):Zs*)*
| Read: "Shared or Overrides, followed by any number of spaces, the whole
| repeated any number of times"
|
| > If I add the last '|.', it works
| > (Shared|Overrides|.):Zs*
| >
| > Any ideas why I need the '|.' as the * at the end of the middle section
| should make
| > it optional ie. 0 is ok
|
| See above.
| BTW: You don't need "|.", but only "|" (i.e. (a|b|c|) makes the whole thing
| optional).
|
| Niki
|
|
 
Back
Top