DEV Community

Ethan
Ethan

Posted on • Edited on

1

Repeated Capturing Group

Regular expression can be used to check a string or a pattern is repeated in a string. For example, if you want to check if the string 'abc' is repeated for exactly 3 times in a string, you can use the following regex: (abc)\1{2}, or it would be like this in Java after adding the escape characters:

Pattern.compile("(abc)\\1{2}");
Enter fullscreen mode Exit fullscreen mode

The \1 in the regex matches the first capturing group in the regex. If you want it to match the second capturing group, you can use \2 and so on.

It is also possible check if a capturing group is repeated at least n times or more than n times. For examples,

  • to check if abc is repeated in a string for at least 5 times, (abc)\1{4,}
  • to check if abc is repeated in a string for less than 5 times, (abc)\1{0,4}

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more