Regex to match phone number 5ABCDYYZZ [closed]

2024/11/19 0:39:52

I am using regex to classify special numbers.

The pattern I wish to match looks like this : 5ABCDXXYY.

Its a number that:

  • starts with "5"
  • followed by 4 different digits (ABCD)
  • then 2 matching digits (XX)
  • and again 2 matching digits (YY)

examples the regex should match: 590631122, 510367722 examples the regex shouldn't match: 566781100, 519283412

I have tried the below

S_E_4_A = 590631122
S_E_4_A_pattern = re.sub(r'.{4}\d(\d)\1(\d)\2', "Special-Private", str(S_E_4_A))
print(S_E_4_A_pattern)

The problem with my regex is that it also match other patterns such as 5ABXXYYZZ.

I want to change my regex to match only two sets of repeating numbers and not more.

Thank you

Answer

You may use this regex to validate your constraints:

\b(?!.{0,3}(\w)\1)5\w{4}(\d)\2(\d)\3\b

RegEx Demo

RegEx Breakup:

  • \b: Word boundary
  • (?!.(\w)\1): Negative lookahead to ensure we don't have a repeat of any chat after 0 to 3 chars
  • 5: Match digit 5
  • \w{4}: Match 4 word characters
  • (\d): Match a digit in capture group #2
  • \2: Match same value as in capture group #2
  • (\d): Match a digit in capture group #3
  • \3: Match same value as in capture group #3
  • \b: Word boundary
https://en.xdnf.cn/q/120013.html

Related Q&A

Remove an \\n\\t\\t\\t-element from list

I got the following list called "phonenumbers". I struggle to remove the elements which contain \n\t\t\t and \n\t\t\t\t. I tried "try and except"-methode and remove(\n\t\t\t\t) but …

Find all numbers in a string in Python 3 [duplicate]

This question already has answers here:How to extract numbers from a string in Python?(20 answers)Split Strings into words with multiple word boundary delimiters(31 answers)Closed 8 years ago.Newbie h…

call dictionary from one function to another

How can I call a dictionary created in one function to another?I have tried using How do I access a dictionary from a function to be used in another function? but it doesnt work for me.I have created…

How to change values in numpy array

import numpy as np a=np.array([[4,2,6],[3,6,5]]) b=np.array([3,5])I want to update the numbers in "a" which are bigger than the numbers in "b" to np.nan. If they are smaller or equa…

Why use the object oriented approach in matplotlib for visualizing data? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Set a status for a discord bot

I am trying to get my bot to have the status, m!help. I see that a lot of other bots have their help command in their status so I wanted to do that too. await bot.change_presence(activity=discord.Game(…

Can you permanently change python code by input?

Im still learning python and am currently developing an API (artificial personal assistant e.g. Siri or Cortana). I was wondering if there was a way to update code by input. For example, if I had a lis…

Count the number of files with a special suffix in a directory using python

It is possible to count the number of all files in a directory by:import ospath = /mnt/BIGDATA/num_files = len([f for f in os.listdir(path) if os.path.isfile(os.path.join(path, f))])As mentioned in How…

How to not hardcode function in this example

The following links contain 2 csv files that the function should pass through grades_1e_2a grades_2e_4aHowever my function is only able to pass the 2nd linked file, as it is hardcoded to range(4,8). ou…

How to group array based on the same values

Please, confused with array in python. I want to group array based on the same values.Code:enter image description hereid_disease = [penyakit_tepung,hawar_daun] for id_disease in id_disease:qres = acac…