Reputation: 1462
I am trying to capture multiple values that will be in the following format:
prof:
prof1
prof2
prof3
...
I don't know how many there will be in the list, it's also possible there will be no values either, but what I want to capture are prof1, prof2, prof3, etc without the whitespace on either side. I have a starter regex:
prof:\s*([\w-]*)
This captures the first prof value, but none of the others. If I add a * at the end of the capture group, none of them are captured. If I add [] on either side of the capture group, it results in an error where it can't figure out what the closing parentheses is for.
Basically, the pattern is, some amount of whitespace, capture text, some amount of whitespace, capture text, etc. But I can't figure out the proper regex for that to work.
Upvotes: 1
Views: 47
Reputation: 163362
Another option could be to match prof:
and capture all after in a capturing group and making sure that there are 1+ empty lines between the prof1, prof2 etc..
Then split that group on 1+ whitespace chars \s+
\bprof:[ \t]*((?:(?:\n[ \t]*$)+\n[ \t]+[\w-]+)*)
Explanation
\bprof:[ \t]*
Word boundary, match prof: followed by 0+ tab/spaces(
Capture group 1
(?:
Non capturing group
(?:\n[ \t]*$)+
Match 1+ times a newline, 0+ tab/spaces and assert end of string\n[ \t]+[\w-]+
Match newline, 1+ tabs/spaces, 1+ wordchars/hyphen)*
Close non capturing group and repeat 0+ times)
Close capture group 1const regex = /\bprof:[ \t]*((?:(?:\n[ \t]*$)+\n[ \t]+[\w-]+)*)/m;
const str = `prof:
prof1
prof2
prof3
...`;
let res = str.match(regex)[1].split(/\s+/).filter(Boolean);
console.log(res);
Upvotes: 0
Reputation: 27723
I'm guessing that this expression in m
mode might be an option, not sure though:
([\s\S]*?)(prof:)|([\w-]*)
The expression is explained on the top right panel of this demo, if you wish to explore/simplify/modify it, and in this link, you can watch how it would match against some sample inputs step by step, if you like.
Upvotes: 0