Regex check for specific phone phone numbers or extensions - regex

String to be evaluated will be either be a 10 digit number or a 4 digit number.
5551119900 (10 Digit)
9999 (4 Digit)
Need regex to test for specific list of 10 digit numbers or 4 digit numbers. I have the following Regex that almost works
55511199(00|01|02|10|20|30)|(0000|9901|9902|9903|9999)
Above is checking for
5551119900
5551119901
5551119902
5551119910
5551119920
5551119930
0000
9901
9902
9903
9999
ISSUE:
(1) Need match to be exactly 10 digits or 4 digits only.
(2) Pattern match (see link below) is showing an exact match and also a "Group 1". I'm not sure what the group match means or if that is a good thing.
Sample: https://regex101.com/r/BbplFG/1/

Try this version of your regex:
^(?:55511199(?:00|01|02|10|20|30)|(?:0000|9901|9902|9903|9999))$
Demo
I have made several changes here:
Used ?: inside terms in parentheses, to turn off group capturing
Placed the entire pattern inside parentheses
Added starting (^) and ending ($) anchors around the entire pattern

Related

Notepad++ replace specific number with up to it's first 4 digit

I want to find those number which contains more than 5 digits and replace it with first 4 digits.
Used below Regex to find number which contains more than 5 digits.
[0-9]{5,}
How Can I achieve blow output?
99999999 -> this will replace with 9999
12345.66 -> this will replace with 1234.66
1234 -> Remains unchanged
This one should do it:
The regex
([0-9]{4})[0-9]+
takes the four numbers as first (and only) group
requires at lease one more number behind
replaces the complete match with the first (and only) group
Using notepad++, you can match 4 digits, then use \K to clear the current output buffer and match 1 or more digits.
\d{4}\K\d+
See a regex demo.
In the replacement use an empty string.
If you don't want partial matches, you can add word boundaries \b around the pattern.
\b\d{4}\K\d+\b
See another regex demo

Regular Expression that needs to match three exact digits in a four digit number

The regular expression that I am trying to create should match all numbers that contain three '8's in any 4 digit number. The regular expression that I have only matches the first 10 numbers out of the list of 15 numbers. Any suggestions will be greatly appreciated.
\b[0-9]*(?:8[0-9]*[0-9]?8|8[0-9]*[0-9]?8|8[0-9]*[0-9]?8)\b
Test data:
8088 8188 8288 8388 8488 8808 8818 8828 8838 8848 8880 8881 8882 8883 8884
The last five numbers should also match, but don't.
You can use
\b(?=\d{4}\b)(?:[0-79]*8){3}[0-79]*\b
See the regex demo.
Details:
\b - a word boundary
(?=\d{4}\b) - there must be 4 digits immediately on the right and they should be followed with a word boundary
(?:[0-79]*8){3} - three occurrences of any 0 or more digits but 8 and then 8
[0-79]* - any 0 or more digits but 8
\b - word boundary.
If it's guaranteed that the number is a four-digit number, then you can try the following:
\b8*[0-79]8*\b
To analyze what each part matches, you can check using,
\b(8*)[0-79](8*)\b
This should do it. This will match any of the 4 patterns.
([\d888]|[8\d88]|[88\d8]|[888\d])
You may want to add a check for the delimiter (in your example the space) as this pattern will match across the spaces giving you many more results
\b(\d?8{3}\d?)\b
this makes the first and last digit in the word bound optional, use
either ? or {0,1}
add quantifier to your eight to have exactly
number of eights you need {3}
replace [0-9] with \d as
Digit for brewity
supposed you have only numbers of length 4. Otherwise use an alternative without optional digits: \b(\d8{3}|8{3}\d)\b

Regex last occurence of digit before some string

I'm trying to create regex to retrieve last number if there was a number or any number if there wasn't any from a string.
Examples:
6 łyżek stopionego masła -> 6
5 łyżek blabla, 6 łyżek masła -> 6
5 łyżek mąki lub masła -> 5
I'm matching only on masła (changing variable) so it has to be included in regex
EDIT:
I cannot explain what I actually need:
Here is regex101 example: https://regex101.com/r/pEeRk3/1
EDIT2:
Emma's solution works great, but I would need to parse decimals and 2multiple digit numbers as well, meaning that those would match as well:
https://regex101.com/r/pEeRk3/3 - I added examples with answers in the link
If you want to match the last occurence of a digit with a decimal and you word has to follow this value, you might use lookarounds:
(?<!\S)\d+(?:\.\d+)?(?!\S)(?!.*\d)(?=.*masła)
(?<!\S)\d+(?:\.\d+)?(?!\S) Match 1+ digits with an optional past to match a dot and 1+ digits
(?!.*\d) assert that there are no more digits following
(?=.*masła) Assert what is on the right is your word
Regex demo
Or you might use a capturing group:
(?<!\S)(\d+(?:\.\d+)?)[^\d\n]* masła(?!\S)[^\d\n]*$
Regex demo
This expression might simply suffice:
.*([0-9])
if we are interested in one digit only, or
.*([0-9]+)
if multiple digits might be desired.
Demo 1
If those strings with masła are desired, we can expand our expression to:
(?=.*masła).*([0-9])
Demo 2
If we would not be validating our numbers and our number would be valid, with commas or dots, then this expression might likely return our desired output:
(?=.*masła)([0-9,.]+)(\D*)$
Demo 3

Regex for the first 8-digit number in a block of text excluding numbers with > 8 digits

I need to check a block of text from an email for a number that's exactly 8 digits long, and only return the first match.
Here are my test cases:
Test123456789 -- should fail because 9 digits
Test23456789Test -- pass
Test23456789 Test -- pass
13456780Test -- pass
Test0123456 -- fail because 7 digits
Extra text in the email: I’ve attached the information you requested. If you have any questions, please let us know. -- extra text in the email shouldn't matter.
I've tried:
.*(\d{8}).* -- matches multiples
.*?(\d{8}).* -- only one match but it also matches on a 9 digit number
.*(?<!\d)\d{8}(?!\d).* -- I found in another answer but it returns all of the text in the email and I only want the 8 digit number.
Thank you for any guidance!
You can use the following regex:
(?!.*\d{9})\d{8}
It starts by using a negative look ahead for 9 digits. Then it matches 8 digits.
This will fail if there's 7 or 9 digits.
A small tweak to the last version you posted:
Try: .*(?<!\d)(\d{8})(?!\d).*
Demo
As the others have said, you can use negative look ahead and negative look behind, and remember to not include a g flag or else you'll match every occurrence of the pattern:
(?<!\d)\d{8}(?!\d)
Demo (global match)
You could find the first occurrence using:
^[\s\S]+?(?<!\d)(\d{8})(?!\d)
That will match:
^ Assert start of the string
[\s\S]+? Match any character non greedy
(?<!\d) negative lookbehind to check what is on the left is not a digit
(\d{8}) Capture 8 digits in a group
(?!\d) Negative lookahead to check what is on the right is not a digit
Or enable make the dot match the newline in your tool or language or prefix the regex with (?s) and replace [\s\S]+? with .+?
Your value is in the first capturing group.
Regex demo

Capture optional second group of digits (non-repeating) using regex

I have a huge dataset, where I am trying to extract a group of 4 digits. The problem is, sometimes there will be a preceding group of 4 digits that I don't want. These 2 groups will never be the same as each other.
Example:
String String 7777 Some more string
String 1234 7777 Some more string
In both cases, I want to extract ONLY 7777 (or whatever digit combination replaces it). There is no pattern to distinguish which number group will be in which position - any number from 0000 to 9999 can be in either first or second position.
If this were possible, I think it'd do what I want?
\b\d{4}{0,1}\s{0,1}(\d{4})\b
Optional 4 digits, optional space, capture 4 digits. But I've tried it, and some variations of it, but I can't get it to work!
A look-ahead seems like a possible candidate, but I don't understand how to construct the pattern.
You can use a negative look-ahead to check if there is no subsequent 4-digit number after it:
\b\d{4}\b(?!\s?\d{4}\b)
See demo
EDIT:
To capture 4-digit number that is not followed by any text and another 4-digit number, you should use:
\b\d{4}\b(?!.+\b\d{4}\b)
See demo
You can use this expression that matches the four digit group not followed by any other four digit groups:
\d{4}(?!.+\d{4}.+)
Online test here.