Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Please help me come with an expression!

I require to search for a space which is not part of double quotes. How do I do this?

Consider this case......Please help


Here's the scenario

There are a list of codes seperated by "comma" like so:
codes: "Code1 code2 F/UN=\"value1 value2\" code3 code4"
Now I need to split this string such that i get all the list of codes into an array of somekind,i.e. the elements of the array should be
[0]: Code1
[1]: Code2
[2]: F/UN=\"value1 value2\"
[3]: code3
[4]: code4

i.e, i require to find a regular expression such that we can split on the basis of those spaces which are not part of quotes.

Can you please help me resolve this issue.

Thanks & Regards

arjun

Published Tuesday, December 26, 2006 10:20 PM by arjun2u

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

Tom Pester said:

I get what you want now. Its not simple what you ask. I remember reading about it in Jeffrey Friedl book about regex. Here is a link that may help you http://groups.google.com/groups?q=regular%20expression%20split%20ignore%20inside%20quote&hl=en&lr=&safe=off&client=opera&rls=en&sa=N&ie=UTF-8&oe=UTF-8&tab=wg In the n1 tool regexbuddy there is also this snippet : ("[^"\r\n]*")?,(?![^",\r\n]*"$) The capturing group makes sure delimiters between double quotes are ignored. The negative lookahead makes sure delimiters between double quotes in the last field are ignored.
December 26, 2006 5:07 PM
 

arjun2u said:

The given snippet has done half the job for me. thx But again its failing in cases like string: code1 code2 f/un="value1 value2","value3" code3 code4 f\ab="value1 value2" code5 because of the "$" which is searching for occurrence at boundary rite? In this case also the list of codes after splitting should be something like this: [0]: code1 [1]: code2 [2]: f/un="value1 value2","value3" [3]: code3 [4]: code4 [5]: f\ab="value1 value2" Devil: code5 Can you consider this case, if its not much of trouble. thanks
December 27, 2006 3:40 AM
 

Brendan said:

arjun, You should probably move this over the the regex advice forums. You will get more help if you post your question in the forums than you will here. Brendan
December 28, 2006 2:02 PM
 

Wes said:

You can give this pattern a try I believe it will do what you want. string pattern = @" (?:^| ) # Element begins with the string begining or space (? (?(OPEN) # If quotes are open Then (""(?<-OPEN>)|.) # Match any char and if a quote then close | # Else (""(?)|[^ ]) # Match any char except the delimter and if quote then open )+ # Match one or more such chars ) (?: |$) # Element ends with space or string ending"; int ci = 0; foreach (Match m in Regex.Matches(input, pattern, RegexOptions.IgnorePatternWhitespace)) Console.WriteLine("{0}: [{1}]", ci++, m.Value);
December 28, 2006 7:20 PM

Leave a Comment

(required) 
(optional)
(required) 
Enter the code you see below

Submit

This Blog

Syndication

Tags

No tags have been created or used yet.