Groups | Blog | Home
all groups > visual studio .net ide > january 2004 >

visual studio .net ide : Replace Glitch with RegExp


Lee Silver
1/29/2004 5:26:52 PM
Given the following Regular Expression:

^:Wh*Stuff

and the following Replacement:

OtherStuff

I would expect that occurances of "Stuff" that are preceded by nothing but
white-space and anchored to the beginning of the line would be replaced by
"OtherStuff". The problem is that the anchoring does not occur -- *all* the
white-space on the current line and previous lines that immediately precede
"Stuff" is replaced.

The workaround is to specify the RegExp as:

^[ \t]*Stuff

--
// Lee Silver
// Information Concepts Inc.
// http://www.information-concepts.com

Facilitating the automated conversion of Data into Information
since 1982
yhhuang NO[at]SPAM online.microsoft.com
1/30/2004 8:00:00 AM
Hello Lee,

Thanks for posting in the group.

Based on my understanding, now the question is: You want to verify the
occurances which are preceded by nothing but white-space and anchored to
the beginning of one line. However, you find ^:Wh*Stuff doesn't work, and
^[ \t]*Stuff works. Please feel free to post here if I have misunderstood
anything.

The complete list of metacharacters and their behavior in the context of
regular expressions are listed at:
http://msdn.microsoft.com/library/en-us/script56/html/js56jsgrpRegExpSyntax.
asp?frame=true

From it, we can see that, ^ matches the position at the beginning of the
input string, which is right. However, :Wh doesn't mean white space there.
Instead, \s matches any white space character including space, tab,
form-feed, and so on. Equivalent to [ \f\n\r\t\v]. So I think ^[ \s]*Stuff
works for you too.

Also, if we input :WhhhStufffdsf in the begining of one line, it matches
^:Wh*Stuff.

Does that answer your question?

Ps: We can send post notify email to you when there are useful replies to
your post in the newsgroup. If you want to receive it in the future, please
register a no spam email alias at:
http://support.microsoft.com/default.aspx?scid=/servicedesks/msdn/nospam.asp
&SD=msdn.

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! ¨C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
Lee Silver
1/30/2004 12:43:56 PM
YanHong:

Thanks for the response.

You understand my problem. If \s* will work in place of [ \t]* (or even :Wh*),
that's even better -- less to key in.

When you click Help Replace dialogue and then rhe Regular Expressions link there
are a number of :xx thingies that are short-hands for Unicode-character
properties. Among them is :Wh which states ":Wh Matches all types of white
space, including publishing and ideographic spaces." I'd also note that that
same page does *not* show \s at all.


--
// Lee Silver
// Information Concepts Inc.
// http://www.information-concepts.com


Facilitating the automated conversion of Data into Information
since 1982
[quoted text, click to view]
Lee Silver
1/30/2004 2:00:25 PM
YanHong:

As a follow-up, I tried \s and it doesn't do anything -- the Replace-dialog told
me that the requested text wasn't found. I changed the \s back to [ \t] and all
worked well; so I suspect the documentation is correct and that \s is not a
short-hand for anything.

--
// Lee Silver
// Information Concepts Inc.
// http://www.information-concepts.com

Facilitating the automated conversion of Data into Information
since 1982


[quoted text, click to view]
Edward Diener
1/31/2004 9:03:25 PM
[quoted text, click to view]

What is "Help Replace dialogue" ? Is it possible its functionality doesn't
match the .NET Regex syntax ?

[quoted text, click to view]

Lee Silver
2/1/2004 12:44:33 PM
Edward:

[quoted text, click to view]

Bad choice of wording on my part. I was referring to click on the Help button on
the Replace dialogue. It brings up a page with a link to the IDE's Regex syntax,
which may be different than that of .NET.

--
// Lee Silver
// Information Concepts Inc.
// http://www.information-concepts.com

Facilitating the automated conversion of Data into Information
since 1982
yhhuang NO[at]SPAM online.microsoft.com
2/2/2004 7:21:27 AM
Hello Lee,

I tested \s on my side and it works fine.

For detailed list on .NET compatible metacharacters, please refer to this
page: http://www.regexlib.com/CheatSheet.htm. It has links to different
MSDN pages.

Also, you can go to http://www.regexlib.com/RETester.aspx to use the small
tool provided to test it.

Thanks very much.

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! ¨C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

This response contains a reference to a third-party World Wide Web site.
Microsoft is providing this information as a convenience to you. Microsoft
does not control these sites and has not tested any software or information
found on these sites; therefore, Microsoft cannot make any representations
regarding the quality, safety, or suitability of any software or
information found there. There are inherent dangers in the use of any
software found on the Internet, and Microsoft cautions you to make sure
that you completely understand the risk before retrieving any software from
the Internet.
Lee Silver
2/2/2004 9:12:35 PM
Yan-Hong:

First, thanks for the cheat-sheet link.

\s does ineed skip white-space. The problem is that ^\s doesn't work. If I
specify \s*text the IDE will find text preceded by white-space; but if I specify
^\s*text then nothing is found.

--
// Lee Silver
// Information Concepts Inc.
// http://www.information-concepts.com

Facilitating the automated conversion of Data into Information
since 1982


[quoted text, click to view]

bullshark
2/2/2004 10:29:40 PM
[quoted text, click to view]

It won't work in my IDE either but

^[:Wh]*text works fine.

I'm not too sure about the \s. It doesn't seem to be documented.

regards,

bullshark
yhhuang NO[at]SPAM online.microsoft.com
2/3/2004 5:28:53 AM
Hello Lee,

Thanks for your update.

Could you please email me your testing sample? After reproducing it on my
side, I will pass it to our product team.

You can reach me by removing online from my email address here. Thanks.

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! ¨C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
Lee Silver
2/3/2004 5:23:40 PM
bullshark:

I just tried this and it doesn't quite work, but thanks for the suggestion (the
thread started with me using a similar regexp, just no brackets). It not only
find 'text' preceded by arbitrary white-space and the start of the line, it also
selects the previous line if all it has is \n. OTOH, ^[ \t]*text works just
fine, just more keystrokes.


--
// Lee Silver
// Information Concepts Inc.
// http://www.information-concepts.com

Facilitating the automated conversion of Data into Information
since 1982


[quoted text, click to view]
Lee Silver
2/3/2004 5:31:34 PM
Yan-Hong Huang:

I'm not sure what type of sample I can give you. I'm in the IDE trying to do a
search and replace of some text in my source-files. A sample snippet from a
source-file is as follows (the lines between the rows of ===.)

=============

Dim Result as Short
Dim Amount As Short
=============

Given the following regexp:

^:Wh*Dim Result

I claim that what should be selected is " Dim Result". Instead, the
previous empty line is also selected.

Also, using your suggestion, the regexp:

^\s*Dim Result

selects nothing and displays the dialog that 'the specified text was not found'

--
// Lee Silver
// Information Concepts Inc.
// http://www.information-concepts.com

Facilitating the automated conversion of Data into Information
since 1982


[quoted text, click to view]
yhhuang NO[at]SPAM online.microsoft.com
2/4/2004 3:07:57 AM
Hello Lee,

Thanks for your reply. Now I know it happens in VS.NET IDE. I reproduced it
exactly.

I will pass it to our VS.NET product group. Thanks very much for your
feedback on our product.

If you have any more concerns on it, please feel free to let me know.

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! ¨C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
yhhuang NO[at]SPAM online.microsoft.com
2/5/2004 1:32:17 AM
Hello Lee,

After consulting our product group, I finally got the right answer for this
question. :)

The expression ^:Wh*Dim Result in our VS Regular expression view breaks
down into the following:
At the beginning of the line (^), one or more white-space occurances (:Wh*)
followed by the words "Dim Result". Note that white space means any white
space characters, including line breaks, tabs, etc.

In your example, we returned the line previous to "Dim Result as Short" as
well as "Dim Result" because that empty line had a whitespace (line break)
at the beginning of the line. This was then followed by the white space
(tab) at the beginning of the "Dim Result" line. Because of the "^:Wh*",
we wanted zero or more occurances of a white space at the beginning of the
line. Therefore, both lines were selected.

Also, it should be noted that in VS Find/Replace, we use our own version of
regular expressions. The Visual Studio Expression syntax can be found
under the help topic
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/vsintro7/html/vxgrfRegularExp
ressionsS.htm
(An easy way to get to this topic is click the expression builder button
(the right arrow located beside the Find What: combobox), then click on
"Complete character list")

In the example above, since the white spaces before "Dim Result" was
actually just a tab, then ^[\t]*Dim Result would return ok, since we are
looking for zero or more occurances of a tab at the beginning of a line
followed by Dim Result.

For ^[\s]*Dim Result, it returned "Specified Text wasn't found" because if
you look at our supported syntax, \s is not supported. In essense, you
were just looking for zero or more occurances of "s" at the beginning of
the line followed directly by Dim Result.

For us, the solution is to explicitly state that you want the search to be
contained in a line. This can be done by the following regular expression
^~(\n):Wh*Dim Result This statement says give me any match where the
beginning of the line doesn't contain a line break, contains zero or more
occurances of any white space, followed by Dim Result.

Does that answer your question? Thanks very much for working with us so
closely.

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! ¨C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
Lee Silver
2/5/2004 5:47:42 PM
Yan-Hong:

Thanks for the follow-ups.

The answer you were given -- that :Wh space matches any white space characters,
including line breaks, tabs, etc. -- makes sense when I think about it; and it
agrees with the documentation.

Since the idea behind my trying to use ^:Wh was to minimize key-strokes, your
proposed solution -- ^~(\n):Wh* -- used too many for me; I'll stick to ^[ \t]*.

--
// Lee Silver
// Information Concepts Inc.
// http://www.information-concepts.com

Facilitating the automated conversion of Data into Information
since 1982


[quoted text, click to view]
yhhuang NO[at]SPAM online.microsoft.com
2/6/2004 1:20:08 AM
Hi Lee,

Thanks for your feedback. I am glad that the question is clarified. If you
have any more concerns on it, please feel free to post in the group.

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! ¨C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
AddThis Social Bookmark Button