views:

212

answers:

7
string patname ="enter pat name";
patname = "text";
patname = " text pat";

Hi, I want to search "pat" in the above statement using regular expression of visual studio. I tried using Pat{([^a-zA-Z]|$)} this , but it will also search the 2nd line which i really don't want. I want pat to be search inside a string value which starts from " follwoed by any char and then "pat". I mean pat can come anywhere ,i.e. either in start,middle or end.

does somebody hav idea how to do tht?

A: 

should be something like:

/.*pat.*/

which . indicates wild char in regex, and .* means any number of occurence of any character

xandy
A: 

How about using word boundaries?

In Visual Studio:

<pat>

Re-reading your question you'll only want the 'pat' inside a string so

"[^"]*<pat>[^"]*"

is probably a better expression.

Huppie
+2  A: 

So you want to look for "pat" in a string in your source code? There are some issues with that... in particular:

1: handling the quotes is awkward (to separate the literal part of the string as opposed to variables etc) - especially with 2 types of escaping; @"""" and "\""

2: in a verbatim string literal, pat could be on the next line but still in the string:

string s = @"abc
def pat ghi";

However, for a rough search, something like:

\".+pat

may help

Marc Gravell
A: 

If I understood you correctly regex you are looking for is just simply

\".+\bpat\b

If you want whole line to be found use

.*\".+\bpat\b.*

To clarify: \b's mean word boundaries. If you want 'pat' to be a part of a word remove them.

samuil
A: 

it doesn't work , i need to search pat inside a string value so i think search shud start somethign like \"[a-z][pat], but the problem is tht it doesn't seach the pat inside ("enter pat name")

Why should it? \"[a-z][pat] means "quote followed by a single alpha, followed by any one of p/a/t". Which isn't what you mean, and isn't what you asked for in the original question. It also isn't clear which it" you are referring to here..
Marc Gravell
A: 

Try "[:a ]*<pat>[:a ]*".

Christian Schwarz
A: 

hi guys thnx for ur reply, "[^"][^"]"it works fine, is there a way tht i can search strings which doesn't hav space after pat , i mean if i hav string like "patname" and "pat name" then i woud like to search "patname" rather than "pat name" ?