match last digits in a long number with regex - regex

I'm real newbie when it comes to Regex so apologies if this 'should' be easy.
I need to match the last 6 digits of a number that has the following format
308950 3200 014559
The first 2 groups of numbers will remain constant (308950 3200) and don't need to be extracted. I am only interested in the last 6 digits.
The full number may contain spaces but these need to be optional.
This has to be done in Regex.

Use regex pattern
(?<=\b308950\s*3200\s*)\d{6}\b
or
\b308950\s*3200\s*(\d{6})\b

This should do it even if there are spaces between the digits
^308950 3200[\d\s]*?((\d\s?){6})$
Group 1 will contain the reqired digits with spaces if any

If the leading numbers will remain constant, you can use:
308950 3200\s*(\d{6})
Alternatively, you could use:
(?:\d+\s)+(\d{6})
Also, if the string will be at the end of the input string, consider adding a $ to the end to signify this (to make sure it'll match the end of the string):
(\d{6})$

Related

regex - allow only single digit numbers in a string

I need to match a string that can be of length from 1 to 20 characters maximum, and it contains letters a-g and numbers 1-7. However, the numbers cannot be next to each other - only single digit numbers are allowed.
Valid strings: aabbca1a6, 4gg1g2g1, 1
Invalid string: aabbca16a - theres two numbers next to each other, forming a two digit number 16.
I can match most strings quite easily with [a-g1-7]{1,20}, however i have no idea how to detect when two numbers are next to each other efficiently.
Currently in my program, after parsing through the regex, i'm just going through the whole string again in a loop, making sure there's no 2 numbers next to each other, however i'd prefer if it all could be done with just one (simple) regex.
You can use the answer from the comments by Ulugbek Umirov using negative lookahead at the start of an anchored string asserting not 2 digits to the right.
^(?!.*\d{2})[a-g1-7]{1,20}$
The pattern matches:
^ Start of string
(?!.*\d{2}) Negative lookahead, assert not 2 digits
[a-g1-7]{1,20} Repeat the ranges in the character class 1-20 times
$ End of string
Regex demo
Another option could be asserting the string length and repeat matching in a way that there can not be 2 digits next to each other
^(?=[a-g1-7]{1,20}$)[a-g]*(?:[1-7][a-g]+)*[0-7]?$
Regex demo
The simplest regex and approach is to check if it doesn't match:
.*\d\d.*
The best way to solve this problem is to use this trick
-Check if number between 1-7 and character between a-g and the numbers are not siblings with each other by using this pattern
^[1-7]?([a-g]+[1-7]?)*
-Then you can check the length of string using string methods like (length method in JavaScript)

Regex how can i get only exact part in a string

I should only catch numbers which are fit the rules.
Rules:
it should be 16 digit
first 11 digit can be any number
after 3 digit should have all zero
last two digit can be any number.
I did this way;
([0-9]{11}[0]{3}[0-9]{2})
number example:
1234567890100012
now I want to get the number even it has got any letter beginning or ending of the string like " abc1234567890100012abc"
my output should be just number like "1234567890100012"
When I add [a-zA-Z]* it gives all string.
Also another point is if there is any number beginning or ending of the string like "999912345678901000129999". program shouldn't take this. I mean It should return none or nothing. How can I write this with regex.
You can use look around to exclude the cases where there are more digits before/after:
(?<!\d)\d{11}000\d\d(?!\d)
On regex101
You can use a capture group, and match optional chars a-zA-Z before and after the group.
To prevent a partial match, you can use word boundaries \b or if the string should match from the start and end of the line you can use anchors ^ and $
\b[a-zA-Z]*([0-9]{11}000[0-9]{2})[a-zA-Z]*\b
Regex demo

Regex - matching while ignoring some characters

I am trying to write a regex to max a sequence of numbers that is 5 digits long or over, but I ignore any spaces, dashes, parens, or hashes when doing that analysis. Here's what I have so far.
(\d|\(|\)|\s|#|-){5,}
The problem with this is that this will match any sequence of 5 characters including those characters I want to ignore, so something like "#123 " would match. While I do want to ignore the # and space character, I still need the number itself to be 5 digits or more in order to qualify at a match.
To be clear, these would match:
1-2-3-4-5
123 45
2(134) 5
Bonus points if the matching begins and ends with a number rather than with one of those "special characters" I am excluding.
Any tips for doing this kind of matching?
If I understood requirements right you can use:
^\d(?:[()\s#-]*\d){4,}$
RegEx Demo
It always matches a digit at start. Then it is followed by 4 or more of a non-capturing group i.e. (?:[()\s#-]*\d) which means 0 or more of any listed special character followed by a digit.
So just repeat a digit, followed by any other sequence of allowed characters 5 or more times:
^(\d[()\s#-]*){5,}$
You can ensure it ends on a digit if you subtract one of the repetitions and add an explicit digit at the end:
^(\d[()\s#-]*){4,}\d$
You can suggest non-digits with \D so et would be something like:
(\d\D*){5,}
Here is a guide.

Regex to check for 4 consecutive numbers

Can I use
\d\d\d\d[^\d]
to check for four consecutive numbers?
For example,
411112 OK
455553 OK
1200003 OK
f44443 OK
g55553 OK
3333 OK
f4442 No
45553 No
f4444g4444 No
f44444444 No
If you want to find any series of 4 digits in a string /\d\d\d\d/ or /\d{4}/ will do. If you want to find a series of exactly 4 digits, use /[^\d]\d{4}[^\d]/. If the string should simply contain 4 consecutive digits use /^\d{4}$/.
Edit: I think you want to find 4 of the same digits, you need a backreference for that. /(\d)\1{3}/ is probably what you're looking for.
Edit 2: /(^|(.)(?!\2))(\d)\3{3}(?!\3)/ will only match strings with exactly 4 of the same consecutive digits.
The first group matches the start of the string or any character. Then there's a negative look-ahead that uses the first group to ensure that the following characters don't match the first character, if any. The third group matches any digit, which is then repeated 3 times with a backreference to group 3. Finally there's a look-ahead that ensures that the following character doesn't match the series of consecutive digits.
This sort of stuff is difficult to do in javascript because you don't have things like forward references and look-behind.
Should the numbers be part of a string, or do you want only the four numbers. In the later case, the regexp should be ^\d{4}$. The ^ marks the beginning of the string, $ the end. That makes sure, that only four numbers are valid, and nothing before or after that.
That should match four digits (\d\d\d\d) followed by a non digit character ([^\d]). If you just want to match any four digits, you should used \d\d\d\d or \d{4}. If you want to make sure that the string contains just four consecutive digits, use ^\d{4}$. The ^ will instruct the regex engine to start matching at the beginning of the string while the $ will instruct the regex engine to stop matching at the end of the string.

How to detect exact length in regex

I have two regular expressions that validate the values entered.
One that allows any length of Alpha-Numeric value:
#"^\s*(?<ALPHA>[A-Z0-9]+)\s*"
And the other only allows numerical values:
#"^\s*(?<NUM>[0-9]{10})"
How can I get a numerical string of the length of 11 not to be catched by the NUM regex.
I think what you're trying to say is that you don't want to allow any more than 10 digits. So, just add a $ at the end to specify the end of the regex.
Example: #"^\s*(?[0-9]{10})$"
Here's my original answer, but I think I read you too exact.
string myRegexString = `#"(?!(^\d{11}$)` ... your regex here ... )";
That reads "while ahead is not, start, 11 digits, end"
If it's single line, you could specify that your match must happen at the end of the line, like this in .net ...
^\s*([0-9]{10})\z
That will accept 1234567890 but reject 12345678901.
Do you mean you want to match up to 10 digits? Try this:
#"^\s*[0-9]{1,10}\s*$"
If you are trying to match only numbers that are 10 digits long, just add a trailing anchor using $, like this:
^\s*(?:[0-9]{10})\s*$
That will match any number that is exactly 10 digits long (with optional space on either side).
var pattern =/\b[0-9]{10}$\b/;
// the b modifier is used for boundary and $ is used for exact length
Match something non-numeric after the length 10 string. My regex-foo isn't that good, but I think you've got it setup there to catch a numeric string of exactly length 10, but since you don't match anything after that, a length 11 string would also match. Try matching beyond the end of the number and you'll be good.
This should match only 10 digits and allow arbitrary numbers of whitespaces before and after the digits.
Non-capturing version: (only matches, the matched digits are not stored)
^\s*(?:\d{10})\s*$
Capturing version: (the matched digits are available in subgroup 1, as $1 or \1)
^\s*(\d{10})\s*$
You could try alternation?
^\s*(?\d{1,10}|\d{12,})