Home > OS >  Regular expression with groups repetition
Regular expression with groups repetition

Time:04-11

I'm writing a regex with Python, and I'm facing this problem. My regex is the following : :(\s )"[^:"]*?([^"]:)?[^:"]*?"[^:"]*?([^"]:)?[^:"]*?"([,}]\s*) My goal is to capture text like this :

  1. : "Some : text " and : another",
  2. : "Some " text",
  3. : "Some text : and, repeating : one " and : another",

The above regex works well with the first two texts.

But now, I'm trying to capture the third text with regex like this but it doesn't work at all. :(\s )"([^:"]*?([^"]:)?[^:"]*?)*"([^:"]*?([^"]:)?[^:"]*?)*"([,}]\s*)

Can anyone give me help, please ?

CodePudding user response:

Try this: ^:[\s\S] ?,$

Demo: https://regex101.com/r/GSjMk8/2

Matches every line which start with : and ends with ,

CodePudding user response:

I finally found the solution after some days of research :). I just need to allow colons to be matched by replacing this [^:"] with this [^"]. And that works well. So the entire regex looks like this if someone is facing the same problem: :(\s )"([^"]*?(:?[^"]:)?[^:]*?)"([^:"]*?(:?[^"]:)?[^:"]*?)"([,}]\s*)

  • Related