PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, November 6, 2022

[FIXED] How to ignore some symbols in a regex with named capture groups

 November 06, 2022     regex, regex-group     No comments   

Issue

Suppose that we have a regex with a named capture group like this:

^(?<my_group>00[A-Za-z]{1}|0[A-Za-z]{2}|[A-Za-z]{3})$

In this case, "my_group" will contain either 00x or 0xx or xxx.

Now I want to store only "x-es" in it, so "my_group" should contain x/xx/xxx. But I cannot figure out how to achieve this behavior.

The only thing that kinda worked out was creating a separate "inner" named groups:

^(?:00(?<my_group1>[A-Za-z]{1})|0(?<my_group2>[A-Za-z]{2})|(?<my_group3>[A-Za-z]{3}))$

But in this case, I'm forced to use different group names because it's forbidden to use the same name in majority of the programming languages (but it could be ok since I use | between them, and at most one of these parts can be matched).

How can it be solved? Note that I cannot throw "zeros" away since this regex is used for string matching before splitting to the groups (so only 00x/0xx/xxx can be passed initially).


Solution

You can use

text.matches("(?=.{3}$)0{0,2}(?<mygroup>[A-Za-z]{1,3})")

See the regex demo.

Details:

  • ^ - start of string (implicit in String#matches)
  • (?=.{3}$) - a positive lookahead that requires the string to contain only 3 chars
  • 0{0,2} - zero to two 0 chars
  • (?<mygroup>[A-Za-z]{1,3}) - Group "mygroup": one to three letters
  • $ - end of string (implicit in String#matches).


Answered By - Wiktor Stribiżew
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing