Regex for min 9 numbers - regex

I'm trying to make a javascript regex to match:
required: 9 or more numbers.
optional: dash, forward slash, plus and space characters.
forbidden: any other character
So far I have
^[0-9-+\/\s]{9,}$
The only problem with this (I think) is that it counts the non numeric permitted characters along to reach the minimum 9.
How can I amend it so that it only counts the numbers to reach the minimum 9?

If you want to solve this in a single RE (not necessarily recommended, but sometimes useful):
^[-+\/\s]*([0-9][-+\/\s]*){9,}$
Or, if you want the first and last characters to be digits:
^[0-9](^[-+\/\s]*[0-9]){8,}$
That's: a digit, followed by eight or more runs of the optional characters, each ending with a digit.

You can use lookahead to check if there are 9 or more digits anywhere
^(?=(\D*\d){9,})[\d/+ -]+$
--------------
|
|->match further only if there are 9 or more digits anywhere
OR
^([/+ -]*\d){9,}[/+ -]*$

^([0-9][-+\/\s]*){9,}$ should do.

its simple with look-ahead
Try this pattern:
^(?=([^\d]*\d){9,})[0-9-+\/\s]*$

Related

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 fixed number of characters but any quantity of spaces

I'm validating some input fields. Here's the regex for a simple example:
^\[0-9]\{6,6}\$
In the example, it requires 6 numbers to be input. However, I want to relax the validation a little and allow spaces where necessary, and remove them later - an example might be a bank sort code.
In the UK, a sort code could be written as 123456, or perhaps 12 34 56.
I know I can amend the expression to include a space within the brackets and relax the numbers in the curly brackets, but what I'd like to do is continue to limit the digits so that 6 must always be input, and allow none or more spaces - of course the spaces could be anywhere.
I'm not sure how to approach this - any ideas, help appreciated.
Try this:
^(\d\s*){6}$
It allows 0 or more whitespace characters after every digit.
If you want to limit whitespace to be inside the digits (without leading or trailing spaces):
^(\d\s*){5}\d$
If you allow spaces at any position alongside 6 digits, then you need
^(\s*[0-9]){6}\s*$
See regex demo
The \s* matches any whitespace, 0 or more repetitions.
Note that a limiting quantifier {6,6} (minimum 6 and maximum 6 repetitions) is equal to {6}.
Also, note that you need to double escape the \s as \\s if you pass the regex pattern as a regular string literal.
And if you plan to only allow regular spaces, not all whitespace, just use
^([ ]*[0-9]){6}[ ]*$
I think you want to look at a lookahead expression
This site explains them in more detail
For your example, ^(?=(\s*[0-9]\s*){6})(\d*\s*)$
This looks for any amount of space, followed by a digit followed by any amount of space 6 times.
Other answers I've seen so far only allow a total of 6 characters, this expression will allow any number of spaces but only 6 digits, no more, no less.
Note: ^(\s*[0-9]\s*){6}$ this will also work, without the lookahead expression
JavaScript Example

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.

How can I recognize a valid barcode using regex?

I have a barcode of the format 123456########. That is, the first 6 digits are always the same followed by 8 digits.
How would I check that a variable matches that format?
You haven't specified a language, but regexp. syntax is relatively uniform across implementations, so something like the following should work: 123456\d{8}
\d Indicates numeric characters and is typically equivalent to the set [0-9].
{8} indicates repetition of the preceding character set precisely eight times.
Depending on how the input is coming in, you may want to anchor the regexp. thusly:
^123456\d{8}$
Where ^ matches the beginning of the line or string and $ matches the end. Alternatively, you may wish to use word boundaries, to ensure that your bar-code strings are properly separated:
\b123456\d{8}\b
Where \b matches the empty string but only at the edges of a word (normally defined as a sequence consisting exclusively of alphanumeric characters plus the underscore, but this can be locale-dependent).
123456\d{8}
123456 # Literals
\d # Match a digit
{8} # 8 times
You can change the {8} to any number of digits depending on how many are after your static ones.
Regexr will let you try out the regex.
123456\d{8}
should do it. This breaks down to:
123456 - the fixed bit, obviously substitute this for what you're fixed bit is, remember to escape and regex special characters in here, although with just numbers you should be fine
\d - a digit
{8} - the number of times the previous element must be repeated, 8 in this case.
the {8} can take 2 digits if you have a minimum or maximum number in the range so you could do {6,8} if the previous element had to be repeated between 6 and 8 times.
The way you describe it, it's just
^123456[0-9]{8}$
...where you'd replace 123456 with your 6 known digits. I'm using [0-9] instead of \d because I don't know what flavor of regex you're using, and \d allows non-Arabic numerals in some flavors (if that concerns you).

Regular Expression - Start with 3538 and then contain 8 digits

E.g. match 353812345678 So far I have ^3538{1}[\d]{8} which works but does not restrict the length. How do I make sure the length is only a maximum of 12 digits?
If you want the number to be the only thing in the string: ^3538\d{8}$
If you just want the number in a string: \b3538\d{8}\b
^ is the start-of-string anchor, while $ is the end-of-string anchor, so the first one restricts the number to be the only thing on the line.
In the other one, \b means a word boundary, so it just means no other letters or digits may come directly before or after the number.
Also, note, in your original regex, the {1} is redundant, and [\d] means the same as \d.
^3538{1}[\d]{8}[^\d] will ensure you have 3538 followed by 8 digits and something that is NOT a digit -- thus limiting the length.
Add a dollar sign ($) at the end of the regex:
^3538{1}[\d]{8}$