473,407 Members | 2,315 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Regex help, please - Recognize quoted strings?

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
Nov 16 '05 #1
6 4763
Wes
> 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/
Nov 16 '05 #2
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" <ne********@puzzleware.net> wrote in message
news:uQ**************@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/

Nov 16 '05 #3
Wes
Hello Dave,

It looks like I had a typo in my regular expression (an extra quote) here is the corrected version
(?:\"([^\"]+)\"|\\S+)
but that isn't your problem.

It looks like from the example you have there you are getting the value from m.ToString(). That will actually return the Value of the first group (m.Group[0].Value) which is defaultly the entire sub-string that the match was found in. You can try m.Group[1].Value that will give you the string without quotes.

I just dug up a regular expression I used in the past to split a string at any whitespace but not split if the string is within quotes.

string searchText = "\"two words\" and asdf";
string[] split = Regex.Split(searchText, @"(?<!""\b[^""]*)\s+(?![^""]*\b"")");
foreach (string s in split)
Console.WriteLine(s.Trim('"'));

// Output
two words
and
asdf

It does however leave the quotes on the string but that is taken care of with Trim. I think this may make your job a little easier (that is as long as you don't try to figure out exactly what that regular expression is doing, I still have trouble with it when I don't look at it for a while ;)

HTH
Wes Haggard
http://weblogs.asp.net/whaggard/
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" <ne********@puzzleware.net> wrote in message
news:uQ**************@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/


Nov 16 '05 #4
Wes:

Unfortunately, the new string doesn't work at all. Also,, m.Groups[0].Value
still returns the string in quotes (using the original string you gave me).
I did try to figure out what that pattern is doing - whew! It uses a
character that isn't even documented in the doc I've been using - the "<"
char? I'm going by what's at:
http://msdn.microsoft.com/library/de...gexpsyntax.asp

At this point, this is mostly an intellectual exercise - I have it working
by trimming out the surrounding quotes. Just a little bit of a hack. If
you have something else for me to try, I'd love to try it. I used to be
competent with this stuff, in my old sed, awk, and lex days. But, it's been
a while. If you'd prefer to punt, that's fine, and thanks for all your help
so far.

- Dave

"Wes" <ne********@puzzleware.net> wrote in message
news:ea**************@TK2MSFTNGP10.phx.gbl...
Hello Dave,

It looks like I had a typo in my regular expression (an extra quote) here is the corrected version (?:\"([^\"]+)\"|\\S+)
but that isn't your problem.

It looks like from the example you have there you are getting the value from m.ToString(). That will actually return the Value of the first group
(m.Group[0].Value) which is defaultly the entire sub-string that the match
was found in. You can try m.Group[1].Value that will give you the string
without quotes.
I just dug up a regular expression I used in the past to split a string at any whitespace but not split if the string is within quotes.
string searchText = "\"two words\" and asdf";
string[] split = Regex.Split(searchText, @"(?<!""\b[^""]*)\s+(?![^""]*\b"")"); foreach (string s in split)
Console.WriteLine(s.Trim('"'));

// Output
two words
and
asdf

It does however leave the quotes on the string but that is taken care of with Trim. I think this may make your job a little easier (that is as long
as you don't try to figure out exactly what that regular expression is
doing, I still have trouble with it when I don't look at it for a while ;)
HTH
Wes Haggard
http://weblogs.asp.net/whaggard/
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" <ne********@puzzleware.net> wrote in message
news:uQ**************@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/

Nov 16 '05 #5
Wes
Hello Dave,
Comments inline.
Wes:

Unfortunately, the new string doesn't work at all. Really? I have tested it on the string you gave me and it worked for me at least it matched quoted strings.
Anyway here is a complete sample piece of code that matches quoted and non-quoted strings.

string searchText = "\"two words\" and asdf";
Regex regEx = new Regex("(?:\"([^\"]+)\"|(\\S+))");
foreach (Match m in regEx.Matches(searchText))
{
// If quoted string
string text = m.Groups[1].Value;

// If non-quoted string
if (text == string.Empty)
text = m.Groups[2].Value;

Console.WriteLine(text);
}

// Output
two words
and
asdf
Also,,
m.Groups[0].Value still returns the string in quotes (using the
original string you gave me). m.Groups[1].Value should be the one with no quotes.

I did try to figure out what that pattern is doing - whew! It uses a character that isn't even
documented in the doc I've been using - the "<" char? I'm going by
what's at:
http://msdn.microsoft.com/library/de...ry/en-us/scrip
t56/html/jsgrpregexpsyntax.asp FYI: the link you gave is for VB script regular expression syntax, which is not exactly the same as .Net Regular Expression syntax, which can be found
http://msdn.microsoft.com/library/de...geElements.asp (and the (?<! ) construct is under the grouping constructs section link, it is a negative lookbehind)
At this point, this is mostly an intellectual exercise - I have it
working by trimming out the surrounding quotes. I know but that is part of the reason for me helping people with issues like this so that I can stay intellectually sharp. ;) Plus i hate giving up before the objective is obtained.
Just a little bit of
a hack. If you have something else for me to try, I'd love to try it.
I used to be competent with this stuff, in my old sed, awk, and lex
days. But, it's been a while. If you'd prefer to punt, that's fine,
and thanks for all your help so far.

- Dave


I hope this is what you are looking for.

Wes Haggard
http://weblogs.asp.net/whaggard/
Nov 16 '05 #6
Wes:

That one almost works. It was the @"(?<!""\b[^""]*)\s+(?![^""]*\b"") one
that I was referring to that didn't work.

The new one works.

Thanks!

"Wes" <ne********@puzzleware.net> wrote in message
news:ep**************@TK2MSFTNGP11.phx.gbl...
Hello Dave,
Comments inline.
Wes:

Unfortunately, the new string doesn't work at all. Really? I have tested it on the string you gave me and it worked for me at

least it matched quoted strings. Anyway here is a complete sample piece of code that matches quoted and non-quoted strings.
string searchText = "\"two words\" and asdf";
Regex regEx = new Regex("(?:\"([^\"]+)\"|(\\S+))");
foreach (Match m in regEx.Matches(searchText))
{
// If quoted string
string text = m.Groups[1].Value;

// If non-quoted string
if (text == string.Empty)
text = m.Groups[2].Value;

Console.WriteLine(text);
}

// Output
two words
and
asdf
Also,,
m.Groups[0].Value still returns the string in quotes (using the
original string you gave me). m.Groups[1].Value should be the one with no quotes.

I did try to figure out what that
pattern is doing - whew! It uses a character that isn't even
documented in the doc I've been using - the "<" char? I'm going by
what's at:
http://msdn.microsoft.com/library/de...ry/en-us/scrip
t56/html/jsgrpregexpsyntax.asp

FYI: the link you gave is for VB script regular expression syntax, which

is not exactly the same as .Net Regular Expression syntax, which can be
found http://msdn.microsoft.com/library/de...geElements.asp
(and the (?<! ) construct is under the grouping constructs section link, it
is a negative lookbehind)
At this point, this is mostly an intellectual exercise - I have it
working by trimming out the surrounding quotes. I know but that is part of the reason for me helping people with issues

like this so that I can stay intellectually sharp. ;) Plus i hate giving up
before the objective is obtained.
Just a little bit of
a hack. If you have something else for me to try, I'd love to try it.
I used to be competent with this stuff, in my old sed, awk, and lex
days. But, it's been a while. If you'd prefer to punt, that's fine,
and thanks for all your help so far.

- Dave


I hope this is what you are looking for.

Wes Haggard
http://weblogs.asp.net/whaggard/

Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Gary McCullough | last post by:
What I want to do sounds simple, but it's defeating me. I want to substitute all occurences of a colon : character in a string with an @ character -- unless the : occurs within a single or...
2
by: Robert Oschler | last post by:
Can someone give me a regex expression that will split a sentence containing words and double-quoted phrases, into an array? I don't want the words between the double-quotes to be split using the...
4
by: William Stacey [MVP] | last post by:
Would like help with a (I think) a common regex split example. Thanks for your example in advance. Cheers! Source Data Example: one "two three" four Optional, but would also like to...
1
by: Mark | last post by:
Hi, I've seen some postings on this but not exactly relating to this posting. I'm reading in a large mail message as a string. In the string is an xml attachment that I need to parse out and...
3
by: Luis Esteban Valencia | last post by:
hello quite a simple one if you understand regular expressions vbscript and ..net, probably quite hard if you don't i have a single line input which offers classic search functionality, so if...
7
by: melanieab | last post by:
Hi, I'm trying to use DataView to find the row number in the datatable that contains "Rich" in it so that I can highlight it. It works fine when I enter the entire string (i.e. Richard), but I...
8
by: Bob | last post by:
I need to create a Regex to extract all strings (including quotations) from a C# or C++ source file. After being unsuccessful myself, I found this sample on the internet: ...
17
by: Mark | last post by:
I must create a routine that finds tokens in small, arbitrary VB code snippets. For example, it might have to find all occurrences of {Formula} I was thinking that using regular expressions...
7
by: Nightcrawler | last post by:
Hi all, I am trying to use regular expressions to parse out mp3 titles into three different groups (artist, title and remix). I currently have three ways to name a mp3 file: Artist - Title ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.