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}

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

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay