473,785 Members | 2,421 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Regex help: Split string into words AND double-quoted phrases

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 space (or comma) character
as a delimiter. I can do one or the other, tokenize the words or tokenize
the double-quoted strings, but I can't figure out how to combine the two
into the same regex expression. Note: I *do* want to capture (retain) the
double-quotes.

For example:
dogs cats and "other things".


I want:

1>dogs
2>cats
3>and
4>"other things"

not:

1>dogs
2>cats
3>and
4>"other
5>things"

Thanks.

Aug 1 '05 #1
2 4501
Here is a rough go - still needs some work.

Use the "|" union operator.

The key I think is to put the expression for the "text in quotes" first
in the union, so that the RegExp parser will grab that if it finds it
first.

function fTest()
{
var s='One two "three four" five six';

var r=/(\"[^"]*\"|\w+)/g;

s=s.replace(r," $1|");

a=s.split("|");

alert(a);
}

Aug 2 '05 #2
hi Robert,
Robert Oschler wrote:
Can someone give me a regex expression that will split a sentence containing
words and double-quoted phrases, into an array?
...


how about this one?

var str = "dogs, cat and \"other things\".";
var regX = /("[^"]+")|(\b\w+\b )/g;
var arr = str.match(regX) ;
alert((typeof arr.toSource == "function") ? (arr.toSource() ) : (arr));
so long - peterS.

Aug 2 '05 #3

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

Similar topics

5
27203
by: Blue Ocean | last post by:
In short, it's not working right for me. In long: The program is designed to read numbers from an accumulator and speak them out loud. Unfortunately, the class that contains the method to read off large numbers is only for integers. My intention is to split a String across the Regex of ".". However, this code does not work: private void doRealValueOf(String text) {
3
412
by: Felix Schwarz | last post by:
Hi all, I'm experiencing problems with a regular expression and I can't figure out which words I use when googling. I read the python documentation for the re module multiple times now but still no idea what I'm doing wrong. What I want to do: - Extract all digits (\d) in a string. - Digits are separated by space (\w)
3
2337
by: Craig Kenisston | last post by:
I have the sudden need to split a text that may have any of the following tokens : Words with quotes or double quotes. Words with no quotes at all. Numbers with and without decimal points, no commas allowed, but may contain parenthesis which I would like to keep apart to drop later. They may be separated by comas, spaces or semicolon.
4
1515
by: H | last post by:
This is kind of an followup on oneof my previous questions, and it has with RegEx to do. I have a string containing of several words. What would a good regex expression looklike to get one match on every word ? For example : String myString =" This is the string that stupid H can't split up"; // A good RegEx needed here .. So the result would look something like this ;
17
3980
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher http://forta.com/books/0672325667/
4
3854
by: Cor | last post by:
Hi Newsgroup, I have given an answer in this newsgroup about a "Replace". There came an answer on that I did not understand, so I have done some tests. I got the idea that someone said, that the split method and the regex.replace method was better than the string.replace method and replace function. I did not believe that.
3
8261
by: Craig Buchanan | last post by:
Is there a way to combine these two Replace into a single line? Regex.Replace(Subject, "\&", "&amp;") Regex.Replace(Subject, "\'", "&apos;") Perhaps Regex.Replace(Subject, "{\&|\'}", "{&amp;|&apos;}") Thanks, Craig
4
3207
by: JS | last post by:
I am writing a C# app that needs to parse a sentence entered by the user for a simple boolean search. I need to capture all of the AND words that are not inside of double quotes. However, I am having a heck of a time figuring out a regex for it. Can anyone assist with a regex to find all the AND's not in double quotes? An example sentence might be: red and blue and "crazy elephant" and "orange and red" and stuff.
11
3112
by: Steve | last post by:
Hi All, I'm having a tough time converting the following regex.compile patterns into the new re.compile format. There is also a differences in the regsub.sub() vs. re.sub() Could anyone lend a hand? import regsub
3
4875
by: =?Utf-8?B?TmF2ZWVu?= | last post by:
Not sure if this is the right forum for this question but couldn'd find another newsgroup. I am new to Regular expressions and would like help in deciding which pattern allows me to split a string into sets of words based on capital letter. For e.g. if i have a string "FirstnameLastname" I would like the result to return me Firstname and Lastname. The other conditions of the input string are 1) If whitespace exists, do not return a...
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10325
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.