开发者

RegEx string splitter

What am I doing wrong here? I am trying to extract from this "list"

ARTICLE 11 - Title AA
ARTICLE 22 Title BB
ARTICLE 33
ARTICLE 44 - Title DD
ARTICLE 55 Title EE

all the article numbers and the titles (if any) for each article. The "-" is optional when title exists.

With this RegEx

(article)(\s*)([^\s]*)((\s*)(-)?(\s开发者_如何转开发*)(.*))

I get only 4 items. The item 33 and 44 are considered one article only and this is I suppose just because "ARTICLE 33" has no title.

11|Title AA
22|Title BB
33|ARTICLE 44 - Title DD
55|Title EE

Please see the code here: http://jsfiddle.net/Z94wf/

EDIT

What I expect to get is this:

11|Title AA
22|Title BB
33|
44|Title DD
55|Title EE

Thanks


You second \s* is matching the newline char on the 3rd line, so if you change to explicitly match only space and dash as follows

(article)(\s*)([^\s]+)(([ -]*)(.*))

you get the desired result

http://jsfiddle.net/Z94wf/37/


I can't be sure on all of the forms of your input but what about something with a few less groups and a bit more explicit...

ARTICLE\s+(\d+)[\s-]*(.*)

This should match the starting literal followed by some space followed by the number and then an optional set of spaces and the "-" char and then everything else.


I believe this is what you want

$(document).ready(function(){
    var s = $('#in').val();
    var re = /(article)(\s)([0-9][0-9])((\s|-)*)/gi;
    $('#out1').val(s.replace(re,'$3|$5'));
});

http://jsfiddle.net/jasongennaro/Z94wf/35/


$(document).ready(function(){
    var s = $('#in').val();
    var re = /article\s*(\d+)[ -]*(.*)/gi;
    $('#out1').val(s.replace(re,'$1|$2'));
});


For tracking down issues like this, I find the Regex Powertoy to be very helpful. You can enter sample text and regular expressions and suss out a lot about how the matches are working (or not working). Super-handy:

http://regex.powertoy.org/


Use this regex:

$(document).ready(function(){
    var s = $('#in').val();
    var re = /^article\s+([^\s]+)(?:$|\s*(-\s*)?)/gmi;
    $('#out1').val(s.replace(re,'$1|'));
});

to get your output.

You can see working demo at: http://jsfiddle.net/Z94wf/76/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜