0

I have a string (())(("I am) a(. text"(((( I need to break it down to three parts:

The first group should include anything that is not a \w or \d or " or '. BUT ALSO should be at the start of the string.

The second group should be literally any characters before group 3

The third group should be as the first group but AT THE END of the string

So the groups of the string above should look like this: 1. (())(( 2. "I am) a(. text" 3. ((((

I have tried to do it like this: ^([^\w\d'\"]*)(.+)([^\w\d'\"]*)$ But the thing is (.+) just eates everything...

I understand that my regex is wrong, but I have no idea how to do what I want to do

MrLalatg
  • 573
  • 1
  • 6
  • 20
  • 2
    Make the quantifier non greedy `^([^\w'"]*)(.+?)([^\w'"]*)$` https://regex101.com/r/mGNcCu/1 See https://stackoverflow.com/questions/22444/my-regex-is-matching-too-much-how-do-i-make-it-stop – The fourth bird Apr 22 '20 at 18:13
  • @The fourth bird Thank you! I didn't know about greedy quantifiers at all, now I learned it! Can you post your comment as the answer so I can mark it – MrLalatg Apr 22 '20 at 18:15
  • You are welcome. It is a common issue (and is actually a duplicate of the added link) – The fourth bird Apr 22 '20 at 18:17
  • I may well be the only one who initially read this as the third group must equal the first group, but I think it would be clearer to change, "The first group should" to "The first and third groups should...". That you want to break the string into three parts implies that the first group is at that beginning of the string and the third group is at the end of the string. – Cary Swoveland Apr 22 '20 at 18:39

0 Answers0