I have simple problem with regex, but I haven't idea for resolve them. I have string (in grey this is a label):
cccc
:dddddbbbb
:fggggaaa aa
:ddd dddcccc
:ggggggg
and regex
/(aaa aa|bbbb|cccc)+:([\sa-zA-Z]*)(?:$|\s)/ig
After parsing string 'label' aaa aa
is ignoring, because have space and is taken to second match. I want to do first match labels (with white space or not), colon and anything (with spaces) after insert into second match to next 'label' or end line.
Any suggestions?
I have simple problem with regex, but I haven't idea for resolve them. I have string (in grey this is a label):
cccc
:dddddbbbb
:fggggaaa aa
:ddd dddcccc
:ggggggg
and regex
/(aaa aa|bbbb|cccc)+:([\sa-zA-Z]*)(?:$|\s)/ig
https://regex101./r/mR3vK5/1
After parsing string 'label' aaa aa
is ignoring, because have space and is taken to second match. I want to do first match labels (with white space or not), colon and anything (with spaces) after insert into second match to next 'label' or end line.
Any suggestions?
Share Improve this question edited Jul 9, 2016 at 6:40 Steve 1,6252 gold badges21 silver badges33 bronze badges asked Jul 8, 2016 at 21:44 PeterPeter 50110 silver badges35 bronze badges3 Answers
Reset to default 3If you know all the keys you may use them inside the positive lookahead and match the values with lazy dot:
/(aaa aa|bbbb|cccc):(.*?)(?=$|\s+(?:aaa aa|bbbb|cccc))/gi
See the JS demo:
var block = "aaa aa|bbbb|cccc";
var rx = RegExp("(" + block + "):(.*?)(?=$|\\s+(?:" + block + "))", "ig");
var s = "cccc:ddddd bbbb:fgggg aaa aa:ddd ddd cccc:ggggggg";
while ((m = rx.exec(s)) !== null) {
document.body.innerHTML += m[1] + ": " + m[2] + "<br/>";
}
Pattern explanation:
(aaa aa|bbbb|cccc)
- either ofaaa aa
orbbbb
orcccc
:
- a literal colon(.*?)
- Group 2 matching 0+ any chararacter other than a newline as few as possible up to the first...(?=$|\s+(?:aaa aa|bbbb|cccc))
- (a positive lookahead that limits the.*?
matching)$
- ... end of string|
- or...\s+
- one or more whitespaces followed with...(?:aaa aa|bbbb|cccc)
- any of the three alternatives (inside a non-capturing group used for grouping only, not capturing)
Well so this will do what you want
/(aaa aa|bbbb|cccc)+:(\s*[a-zA-Z]*)(?:$|\s)/ig
But, it is highly contrived, given the question.
If you are trying to get just the labels (even if the key has a space), here's something I quickly typed up: https://regex101./r/jJ9iI1/3
(?:^|\s)([^:]+):
It starts off with a ^
or \s
(beginning or space character). Then it simply captures every character (except the colon) leading up to a colon.
...make sure the g
for global is turned on