I
inpuarg
I want to find a & character using Regex. But not &&
How can i manage this in c# Quickfind window ?
How can i manage this in c# Quickfind window ?
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Dave said:Hi Lee,
Good idea (incorrect syntax though). Based off that pattern I got this to
work:
&~([&:b])
However, it will match the last "&" in "&&" when it's not immediately
followed by a space or tab. In other words, the pattern matches "&", and
the last "&" in "&&", but not, "&& ".
I thought it might be acceptable since it's common for && to be followed by
at least one space in code. If you can't guarantee that, then the pattern
will not work for you.
Just FYI,
[^&]&[^&] and ~(&)&~(&)
didn't work for me, unfortunately.
HTH
--
Dave Sexton
The following untested RegExp might work:
[~&]&[~&]
It should find an & that is neither preceded nor followed by an &.
--
// Lee Silver
// Information Concepts Inc.
I want to find a & character using Regex. But not &&
How can i manage this in c# Quickfind window ?

Dave said:Hi Lee,
Good idea (incorrect syntax though). Based off that pattern I got this to
work:
&~([&:b])
However, it will match the last "&" in "&&" when it's not immediately
followed by a space or tab. In other words, the pattern matches "&", and
the last "&" in "&&", but not, "&& ".
I thought it might be acceptable since it's common for && to be followed
by
at least one space in code. If you can't guarantee that, then the pattern
will not work for you.
Just FYI,
[^&]&[^&] and ~(&)&~(&)
didn't work for me, unfortunately.
HTH
--
Dave Sexton
The following untested RegExp might work:
[~&]&[~&]
It should find an & that is neither preceded nor followed by an &.
--
// Lee Silver
// Information Concepts Inc.
I want to find a & character using Regex. But not &&
How can i manage this in c# Quickfind window ?
Dave said:Hi Lee,
I'm suprised that works for you. I'm using VS 2005 as well and it doesn't
work for me. Actually, both of the examples that didn't work for me matched
absolutely nothing at all - not even a single "&".
I just tested it again. When I tested it the first time I just typed &'s
and &&'s (and some with trailing spaces) along the left margin of an empty
document, one per line. Apparently, [^&]&[^&] only works if there is some
combination of characters before the "&", even if it's whitespace, but it
doesn't just match "&" it makes the whitespace around it as well.
So, for instance, the resulting match would be " & ", not "&".
Also, it's kinda buggy. If I enter "&" along the left margin it doesn't
match. Then, if I insert whitespace before it, other than &'s, it still
doesn't match! I have to delete it and enter some strange combination of
tabs, spaces and characters before it first, then type &, and then it will
match. But even that doesn't work every time, although it seems to find " &
" in "if ((0 & 1) == 0)" with no problem
--
Dave Sexton
Dave:
Yeah, on the syntax, ~ should have been ^ -- that's what I get for
posting un-tested code.
OTOH, I just tried [^&]&[^&] and it works as advertised -- it finds &'s
that are not also &&'s. This works in VS 2005; but I don't know about
VS2003.
Based on what the OP wanted, I would probably modify the RE to
[^&]&[^=&] in order to bypass the &= operator.
--
// Lee Silver
// Information Concepts Inc.
Dave said:Hi Lee,
Good idea (incorrect syntax though). Based off that pattern I got thisto
work:
&~([&:b])
However, it will match the last "&" in "&&" when it's not immediately
followed by a space or tab. In other words, the pattern matches "&", and
the last "&" in "&&", but not, "&& ".
I thought it might be acceptable since it's common for && to be followed
by
at least one space in code. If you can't guarantee that, then the pattern
will not work for you.
Just FYI,
[^&]&[^&] and ~(&)&~(&)
didn't work for me, unfortunately.
HTH
--
Dave Sexton
The following untested RegExp might work:
[~&]&[~&]
It should find an & that is neither preceded nor followed by an &.
--
// Lee Silver
// Information Concepts Inc.
I want to find a & character using Regex. But not &&
How can i manage this in c# Quickfind window ?
I haven't tried it, but I don't see why it wouldn't match the string " & "
Dave said:Hi Lee,
I'm suprised that works for you. I'm using VS 2005 as well and it doesn't
work for me. Actually, both of the examples that didn't work for me
matched
absolutely nothing at all - not even a single "&".
I just tested it again. When I tested it the first time I just typed &'s
and &&'s (and some with trailing spaces) along the left margin of an empty
document, one per line. Apparently, [^&]&[^&] only works if there is some
combination of characters before the "&", even if it's whitespace, but it
doesn't just match "&" it makes the whitespace around it as well.
So, for instance, the resulting match would be " & ", not "&".
Also, it's kinda buggy. If I enter "&" along the left margin it doesn't
match. Then, if I insert whitespace before it, other than &'s, it still
doesn't match! I have to delete it and enter some strange combination of
tabs, spaces and characters before it first, then type &, and then it will
match. But even that doesn't work every time, although it seems to find "
&
" in "if ((0 & 1) == 0)" with no problem
--
Dave Sexton
Dave:
Yeah, on the syntax, ~ should have been ^ -- that's what I get for
posting un-tested code.
OTOH, I just tried [^&]&[^&] and it works as advertised -- it finds &'s
that are not also &&'s. This works in VS 2005; but I don't know about
VS2003.
Based on what the OP wanted, I would probably modify the RE to
[^&]&[^=&] in order to bypass the &= operator.
--
// Lee Silver
// Information Concepts Inc.
Dave said:Hi Lee,
Good idea (incorrect syntax though). Based off that pattern I got this
to
work:
&~([&:b])
However, it will match the last "&" in "&&" when it's not immediately
followed by a space or tab. In other words, the pattern matches "&",
and
the last "&" in "&&", but not, "&& ".
I thought it might be acceptable since it's common for && to be followed
by
at least one space in code. If you can't guarantee that, then the
pattern
will not work for you.
Just FYI,
[^&]&[^&] and ~(&)&~(&)
didn't work for me, unfortunately.
HTH
--
Dave Sexton
The following untested RegExp might work:
[~&]&[~&]
It should find an & that is neither preceded nor followed by an &.
--
// Lee Silver
// Information Concepts Inc.
I want to find a & character using Regex. But not &&
How can i manage this in c# Quickfind window ?

My RE will not find a single & that is in the left or right margin --
but in dealing with realistically entered code, the chances of one in
the left margin I think are nil; and as the right-most character only
marginally bigger.
Dave said:Hi Lee,
I'm suprised that works for you. I'm using VS 2005 as well and it doesn't
work for me. Actually, both of the examples that didn't work for me
matched
absolutely nothing at all - not even a single "&".
I just tested it again. When I tested it the first time I just typed &'s
and &&'s (and some with trailing spaces) along the left margin of an empty
document, one per line. Apparently, [^&]&[^&] only works if there is some
combination of characters before the "&", even if it's whitespace, but it
doesn't just match "&" it makes the whitespace around it as well.
So, for instance, the resulting match would be " & ", not "&".
Also, it's kinda buggy. If I enter "&" along the left margin it doesn't
match. Then, if I insert whitespace before it, other than &'s, it still
doesn't match! I have to delete it and enter some strange combination of
tabs, spaces and characters before it first, then type &, and then it will
match. But even that doesn't work every time, although it seems to find "
&
" in "if ((0 & 1) == 0)" with no problem
--
Dave Sexton
Dave:
Yeah, on the syntax, ~ should have been ^ -- that's what I get for
posting un-tested code.
OTOH, I just tried [^&]&[^&] and it works as advertised -- it finds &'s
that are not also &&'s. This works in VS 2005; but I don't know about
VS2003.
Based on what the OP wanted, I would probably modify the RE to
[^&]&[^=&] in order to bypass the &= operator.
--
// Lee Silver
// Information Concepts Inc.
Dave said:Hi Lee,
Good idea (incorrect syntax though). Based off that pattern I got this
to
work:
&~([&:b])
However, it will match the last "&" in "&&" when it's not immediately
followed by a space or tab. In other words, the pattern matches "&",
and
the last "&" in "&&", but not, "&& ".
I thought it might be acceptable since it's common for && to be followed
by
at least one space in code. If you can't guarantee that, then the
pattern
will not work for you.
Just FYI,
[^&]&[^&] and ~(&)&~(&)
didn't work for me, unfortunately.
HTH
--
Dave Sexton
The following untested RegExp might work:
[~&]&[~&]
It should find an & that is neither preceded nor followed by an &.
--
// Lee Silver
// Information Concepts Inc.
I want to find a & character using Regex. But not &&
How can i manage this in c# Quickfind window ?
Dave said:Hi Lee,
My RE will not find a single & that is in the left or right margin --
but in dealing with realistically entered code, the chances of one in
the left margin I think are nil; and as the right-most character only
marginally bigger.
I just realized something, upon reading your comment again:
string value = @"
&
& &
& & & &";
The above is valid code that won't produce any matches. Given that the OP
is searching strings, it's possible, although unlikely that it will exist.
--
Dave Sexton
Dave:
I haven't tried it, but I don't see why it wouldn't match the string "
& " -- it should match any & preceded and followed by a non-&. OTOH, as
you say, the match will hi-light 3 characters because the RE matches 3
characters. For just doing a search, hi-lighting 3 characters should
not be a problem; and even if doing a Replace, specifying \1 and \2 in
the replacment string (and enclosing each [^&] in the RE in braces
thusly {[^&]}) would be no problem.
On the real code against which I ran the RE it found all single &'s and
skipped all &&'s.
--
// Lee Silver
// Information Concepts Inc.
Dave said:Hi Lee,
I'm suprised that works for you. I'm using VS 2005 as well and it doesn't
work for me. Actually, both of the examples that didn't work for me
matched
absolutely nothing at all - not even a single "&".
I just tested it again. When I tested it the first time I just typed &'s
and &&'s (and some with trailing spaces) along the left margin of an empty
document, one per line. Apparently, [^&]&[^&] only works if there is some
combination of characters before the "&", even if it's whitespace, but it
doesn't just match "&" it makes the whitespace around it as well.
So, for instance, the resulting match would be " & ", not "&".
Also, it's kinda buggy. If I enter "&" along the left margin it doesn't
match. Then, if I insert whitespace before it, other than &'s, it still
doesn't match! I have to delete it and enter some strange combination of
tabs, spaces and characters before it first, then type &, and then it will
match. But even that doesn't work every time, although it seems to find "
&
" in "if ((0 & 1) == 0)" with no problem
--
Dave Sexton
Dave:
Yeah, on the syntax, ~ should have been ^ -- that's what I get for
posting un-tested code.
OTOH, I just tried [^&]&[^&] and it works as advertised -- it finds &'s
that are not also &&'s. This works in VS 2005; but I don't know about
VS2003.
Based on what the OP wanted, I would probably modify the RE to
[^&]&[^=&] in order to bypass the &= operator.
--
// Lee Silver
// Information Concepts Inc.
Dave said:Hi Lee,
Good idea (incorrect syntax though). Based off that pattern I got this
to
work:
&~([&:b])
However, it will match the last "&" in "&&" when it's not immediately
followed by a space or tab. In other words, the pattern matches "&",
and
the last "&" in "&&", but not, "&& ".
I thought it might be acceptable since it's common for && to be followed
by
at least one space in code. If you can't guarantee that, then the
pattern
will not work for you.
Just FYI,
[^&]&[^&] and ~(&)&~(&)
didn't work for me, unfortunately.
HTH
--
Dave Sexton
The following untested RegExp might work:
[~&]&[~&]
It should find an & that is neither preceded nor followed by an &.
--
// Lee Silver
// Information Concepts Inc.
inpuarg wrote:
I want to find a & character using Regex. But not &&
How can i manage this in c# Quickfind window ?
Based on the OP's original message -- and his latest follow-up -- it
looks like he's looking for the bit-wise & operator without also
finding the logical && operator and not really searching strings. My RE
is based on that assumption.
As for your example, the RE should find no hits on the first 2 lines
(with &'s) and 4 hits on the last line (on my system it found the 4
hits).

Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.