-1

This is my regex: ((.+?(?=[(]))([(].+?[)])) and a string template: "string(100)". from that i capture 3 groups. first is "string(100)" 2nd, is "string" and third is "(100)". But i want to exclude brackets from third group, so i only have "100". I have seen all other similar posts, but couldn't succeed to adapt them to my problem.

I apologize for writing almost duplicate post, I just don't have much time to solve it myself.

Thanks in advance.

Nick
  • 455
  • 9
  • 28
  • 2
    Use `((.*?)[(](.*?)[)])`. I.e. `/((.*?)\((.*?)\))/`. Or even ``/(.*?)\((.*?)\)/``, the match is in Group 0. – Wiktor Stribiżew Mar 13 '19 at 13:09
  • 1
    Possible duplicate of [Regular expression to extract text between square brackets](https://stackoverflow.com/questions/2403122/regular-expression-to-extract-text-between-square-brackets) – Nick Mar 14 '19 at 10:46

3 Answers3

0

just match the parenthesis literally and create a group containing only what is inside the parenthesis:

That's one way to solve it:

((.+?(?=[(]))\((.+?)\))
0xAA55
  • 57
  • 6
0

Your expression is probably way more complicated than what it needs to be. This minimalistic RegEx can probably solve your problem:

(.*)\((.*)\)

It matches anything that has a pair of brackets. The whole match can be usually used as a capturing group in all languages/frameworks. The two explicit capturing groups are around everything before the brackets and everything inside. (By "Everything" I mean all characters other than newlines.)

Here's an interactive in-depth explanation and some examples: https://regex101.com/r/OgshEn/1

Hubert Grzeskowiak
  • 15,137
  • 5
  • 57
  • 74
0

This regex '/(\w+)((\d+))/' achieves the desired result. Please refer to following javascript result.

let matches = "string(100)".match(/(\w+)\((\d+)\)/);
console.log(matches[0]);
console.log(matches[1]);
console.log(matches[2]);
RK_15
  • 929
  • 5
  • 11