Re: Regex help, please - Recognize quoted strings?
From: Dave (dpt_at_nospam.eliassen.com)
Date: 10/21/04
- Next message: Brent: "Create a network account from program?"
- Previous message: Leicester B. Ford Jr.: "Re: Passing a type as a parameter"
- In reply to: Wes: "Re: Regex help, please - Recognize quoted strings?"
- Next in thread: Wes: "Re: Regex help, please - Recognize quoted strings?"
- Reply: Wes: "Re: Regex help, please - Recognize quoted strings?"
- Messages sorted by: [ date ] [ thread ]
Date: Thu, 21 Oct 2004 18:03:39 -0400
Thanks, Wes!
The regex string you gave me now solves the big problem - it returns the
entire "phrase" inside the quotes. It still does return the quotes
themselves, though. I can strip those out with a call to Trim(), but that's
a little bit of a hack. Can you figure out how to tell it to strip the
quotes for me?
- Dave
"Wes" <newsgroups@puzzleware.net> wrote in message
news:uQ8KB06tEHA.1472@TK2MSFTNGP10.phx.gbl...
> > I'm struggling with something that should be fairly simple. I just
> > don't know the regext syntax very well, unfortunately.
> >
> > I'd like to parse words out of what is basically a boolean search
> > string. It's actually the input string into a Microsoft Index Server
> > search.
> >
> > The string will consist of words, perhaps enclosed in quotes or
> > parentheses. I'd like to use Regex to pull out the words, or the
> > phrases if the words are enclosed in quotes. Example
> >
> > The string: asdf or qwer or hjkl
> > should yield three results: asdf, qwer, hjkl
> > and:
> >
> > "two words" and asdf
> > should yield two results: "two words", and "asdf"
> > There's the added complexity that the strings may have groups of words
> > surrounded by parentheses, but I think I can figure that out if I
> > solve the quoted strings problem.
> >
> > I've tried a few things, but I can't manage to come up with something
> > that isn't returning the quotes in the return values.
> >
> > Here's some code:
> >
> > Regex regEx("") = new Regex("([\"][^\"]+[\"]|\\S+)");
> >
> > string searchText = "\"two words\" and asdf";
> > foreach (Match m in regEx.Matches(searchText))
> > {
> > string text = m.ToString();
> > MessageBox.Show(text);
> > }
> > In the above code, it will pull out the words, but the text pulled out
> > includes the quotes in "two words";
> > I tried to tell it to match but ignore the quotes, using:
> > Regex regEx("") = new Regex("(?:(\"){1}[^\"]?:(\"){1}|\\S)+");
> > but that doesn't work either. Obviously I don't know what I'm doing.
> >
> > Please help!
> >
> > - Daev
>
> Hello Dave,
>
> With "(?:(\"){1}[^\"]?:(\"){1}|\\S)+" you are saying don't capture the the
whole thing i.e. by '(?:' but you are capturing both quotes individually
with (\").
>
> Try (?:"\"([^\"]+)\"|\\S+)
>
> This should only capture the stuff with quotes around it, excuding the
quotes.
>
> HTH
> Wes Haggard
> http://weblogs.asp.net/whaggard/
>
>
- Next message: Brent: "Create a network account from program?"
- Previous message: Leicester B. Ford Jr.: "Re: Passing a type as a parameter"
- In reply to: Wes: "Re: Regex help, please - Recognize quoted strings?"
- Next in thread: Wes: "Re: Regex help, please - Recognize quoted strings?"
- Reply: Wes: "Re: Regex help, please - Recognize quoted strings?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|