Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Regex to split

Posted on 2011-02-16
8
Medium Priority
?
246 Views
Last Modified: 2012-08-13
I have this string, and need to split it as follows:

HANDLER click, ".ml,.asr,.menu", MenuClick %target%, "asdasdkjfhkasdj,,sdaflijsdalfk", anything

[1] HANDLER
[2] CLICK
[3] ".ml,.asr,.menu"
[4] The rest of the string after the next comma, whatever it is ! So in this case   MenuClick %target%, "asdasdkjfhkasdj,,sdaflijsdalfk", anything

Any ideas on a regex to split (or match) this in javascript ? thanks
0
Comment
Question by:plq
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 20

Expert Comment

by:Proculopsis
ID: 34905245

I would go for a match instead.

  "[^"]*"|[^, ]+
0
 
LVL 8

Author Comment

by:plq
ID: 34905290
Thanks, thats close

Its giving this

myArray[0] = "HANDLER"
myArray[1] = "click"
myArray[2] = "".ml,.asr,.menu""
myArray[3] = "MenuClick"
myArray[4] = "%target%"
myArray[5] = ""asdasdkjfhkasdj,,sdaflijsdalfk""
myArray[6] = "anything "

And what I need is this

myArray[0] = "HANDLER"
myArray[1] = "click"
myArray[2] = "".ml,.asr,.menu""
myArray[3] = "MenuClick %target%, "asdasdkjfhkasdj,,sdaflijsdalfk", anything"

I cannot predict what will be in element [3] - it could be any text, so I don't want the regex to operate on that at all, when its on item 3, just return everything else in the string.. thanks

0
 
LVL 4

Expert Comment

by:andreizz
ID: 34905475
You can test this here:
http://www.regextester.com/

enter the text below in the Type regex: field:
(.[A-Z]+)[ ](.[a-z]+)[,][ ](".[a-z,.]+"),(.*)

Open in new window


And insert the test text in the Test on Text: field.

Good luck!
0
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
LVL 20

Accepted Solution

by:
Proculopsis earned 668 total points
ID: 34905575
I think this gets it:

var source = 'HANDLER click, ".ml,.asr,.menu", MenuClick %target%, "asdasdkjfhkasdj,,sdaflijsdalfk", anything';

var regularExpression = /(?:[, ]*)((?:"[^"]*")|[^, ]+)(?:[, ]*)((?:"[^"]*")|[^, ]+)(?:[, ]*)((?:"[^"]*")|[^, ]+)(?:[, ]*)(.*)/;

var result = source.replace( regularExpression, "$1~~~$2~~~$3~~~$4" );
result = result.split( "~~~" );
var i = -1;
while ( ++i != result.length ) alert( result[i] );

Open in new window

0
 
LVL 4

Assisted Solution

by:andreizz
andreizz earned 664 total points
ID: 34905681
Using RegExp object:
var str = "HANDLER click, ".ml,.asr,.menu", MenuClick %target%, "asdasdkjfhkasdj,,sdaflijsdalfk", anything";
var re = new RegExp("HANDLER click, ".ml,.asr,.menu", MenuClick %target%, "asdasdkjfhkasdj,,sdaflijsdalfk", anything", "g");
var myArray = str.match(re);
Using literal:
var myArray = str.match(/HANDLER click, ".ml,.asr,.menu", MenuClick %target%, "asdasdkjfhkasdj,,sdaflijsdalfk", anything/g)

if ( myArray != null) {
for ( i = 0; i < myArray.length; i++ ) {
var result = "myArray[" + i + "] = " + myArray[i];
}
}

Open in new window

0
 
LVL 9

Assisted Solution

by:iGottZ
iGottZ earned 668 total points
ID: 34905786
^(.+?)\s+(.+?),\s+(".+?"),\s+(.+)$
\1: HANDLER
\2: click
\3: ".ml,.asr,.menu"
\4: MenuClick %target%, "asdasdkjfhkasdj,,sdaflijsdalfk", anything

Open in new window


var string = 'HANDLER click, ".ml,.asr,.menu", MenuClick %target%, "asdasdkjfhkasdj,,sdaflijsdalfk", anything';
string = string.match(/^(.+?)\s+(.+?),\s+(".+?"),\s+(.+)$/);
string.shift();
alert(string.join("\n\n"));

Open in new window

0
 
LVL 8

Author Comment

by:plq
ID: 34906700
gosh thanks for all the responses folks, you guys rock !  Let me test them out and I'll report back here in an hour or two
0
 
LVL 8

Author Closing Comment

by:plq
ID: 34942515
Sorry folks I've got pulled onto another project and this will have to be shelved for about 2 weeks. I'll split points
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question