Solved

Regex to split

Posted on 2011-02-16
8
244 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 20

Accepted Solution

by:
Proculopsis earned 167 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 166 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 167 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

710 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