I'm here trying to simplify a regex.
What i tried was doing a repetition for the same expression, but when I tried to simplify it using ()*, it does not worked as it does not detect the pattern I want.
These are my regex:
(([\(]\w]{1,3}[\)])\s([\d]{1,3}[\?])([\(][\w]{1,3}[\)])\s[\d]{1,3}[\?]([\(][\w]{1,3}[\)])\s([\d]{1,3}[\?])([\(][\w]{1,3}[\)])\s[\d]{1,3}[\?]([\(][\w]{1,3}[\)])\s[0-9]{1,3}[\?]([\(][\w]{1,3}[\)])\s[\d]{1,3}[\?])
Whole pattern:
3A 1?(1) 2?(2) 3?(a) 4?(4) 5?(a) 6?(ii) 7?
4 6?(1) 7?(2) 8?(a) 9?(4) 10?(a) 11?(ii) 12?
These are the patterns it will detect:
1?(1) 2?(2) 3?(a) 4?(4) 5?(a) 6?(ii) 7?
The regex will detect ONLY the first LINE without the 3A. How can I do these?
The regex already be at it's simplest that is \d+\?(?:\([\da-z]+\))?
, and now how can I put it in a way to detect first line only? Thanks guyss.
Maybe, here we can simplify it in another way, if it would be possible. For example, we might have three patterns, which we can capture it using three capturing groups: start, end and middle groups, possibly similar to:
(?:^\w+\s)|(\d\?\(\w+\)\s)|(?:\d+\?$)
If this wasn't your desired expression, you can modify/change your expressions in regex101.com, and add or reduce the boundaries that you wish.
You can also visualize your expressions in jex.im:
const regex = /(?:^\w+\s)|(\d\?\(\w+\)\s)|(?:\d+\?$)/gm;
const str = `3A 4?(1) 5?(2) 6?(a) 7?(4) 8?(a) 9?(ii) 10?`;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}