Regex to match digits and at most one space between them
[\d ?]{7,12}
is a pattern that matches 7 to 12 digit, space or ?
chars. It can match a ???????
string because ?
is not a quantifier, but a mere question mark symbol when declared inside a character class.
If you change it to (?:\d ?){7,12}
, you may partially solve the problem, the space at the end. I suggest using
\b\d(?: ?\d){6,11}\b
See the regex demo
The word boundaries \b
will make sure you only match whole words.
Details
\b
- leading word boundary\d
- a digit(?: ?\d){6,11}
- 6 to 11 consecutive sequences of?
- an optional space\d
- a single digit
\b
- trailing word boundary.
Regex to match all groups between more than one space
You don't need to use look arounds here. Just use this regex to match a non-whitespace string or substring separated by a single space:
\S+(?:\s\S+)*
RegEx Demo
RegEx Details:
\S+
: Match 1+ non-space characters(?:\s\S+)*
: Match 0 or more non-space substring separated by a single space.
Regex for two numbers and space between
You can try something like so: ^\d+(,\d+)?(\s\d+(,\d+)?)+$
, example here.
This assumes that:
- The
,
is your floating point delimeter; - Negative numbers aren't allowed.
EDIT
As per Wiktor's comment, this solution accepts two or more numbers separated by a white space. To only accept 2 numbers, the following needs to be used: ^\d+(,\d+)?(\s\d+(,\d+)?)$
. (Notice the +
at the end has been removed, changing the logic to 1 or more to just 1).
EDIT 2
As per your comment, if you need to match both the ,
and the .
as separator, you would need to use something such as this: ^\d+([,.]\d+)?(\s\d+([,.]\d+)?)+$
. This will accept both characters as decimal separators, however, please note that as is, can accept something of the sort: 12.34 34,56
.
Regex match certain amount of character and allow space inbetween
Your ([0-9\ ?.?]{7,16})
expression matches 7 to 16 occurrences of any character that is either a digit, or a space, or a ?
, or .
. Yes, the ?
inside [...]
is a literal ?
, not a quantifier.
You need to use an expression that will match a digit ([0-9]
) and then exactly 7 sequences of a space or period ([ .]
) followed with 1 digit, and to make sure you are not matching the digits in 123.156.78.146
you may use special boundaries:
(?<!\d[ .]?)\d(?:[. ]?\d){7}(?![ .]?\d)
if the space or .
can only be 0 to 1 in between digits; or - if the space/dot can appear 0 or more times,
(?<!\d[ .]*)\d(?:[. ]*\d){7}(?![ .]*\d)
See the regex demo
The (?<!\d[ .]*)
is a negative lookbehind that will fail any match if it starts with a digit that is followed with .
(s) or space(s), and the (?![ .]*\d)
negative lookahead will fail the match if the 7 digits you need are followed with .
(s) or space(s) and a digit.
A regex to allow exactly 5 digit number with one optional white space before and after the number?
I might suggest just using lookarounds here:
/(?<![ ]{2})\b\d{5}\b(?![ ]{2})/
This pattern says to:
(?<![ ]{2}) assert that 2 (or more) spaces do NOT precede the ZIP code
\b\d{5}\b match a 5 digit ZIP code
(?![ ]{2}) assert that 2 (or more) spaces do not follow
Here is a demo showing that the pattern works.
What is the regex for matching any numbers, with spaces between them?
The RegExp you're looking for is the following:
\b(\d+)\b
The above RegExp will match any number between two word boundaries, and, therefore, it will match:
"123 "
" 123"
" 123 "
Use the g
modifier to match all the occurrences (all the numbers). You can find a working example HERE.
JavaScript example:
var text = "12 Voorbeeld3 4Voorbeeld5 6 test 777",
exp = /\b(\d+)\b/g,
match;
while (match = exp.exec(text)) {
console.log(match[0]);
}
Result:
12
6
777
Regular Expression - Ignore multiple spaces and Consider only one space in the match
You can match single spaces by editing your CircuitID part to either match a space character that isn't followed by another space character (?! )
(negative lookahead), or one of the non-space characters [a-zA-Z0-9\-\/]
.
so the CircuitID part becomes (?<CircuitID>(?:[a-zA-Z0-9\-\/]| (?! )){6,26})
regex101
How can I perform a regex that forces at least one space?
Based on your shown samples, could you please try following.
^(?=\S*\s)[\sa-zA-Z]{1,16}$
Explanation: Using positive look ahead to check if values other spaces coming 1 or more time followed by a space. Then using character class for space alphabets which will have 1 to 16 occurrences till end.
Related Topics
Inserting a Python Datetime.Datetime Object into MySQL
Flask Installed, But Modulenotfounderror: No Module Named 'Flask'
_Corrupt_Record Error When Reading a Json File into Spark
Best Practices for Adding .Gitignore File for Python Projects
Stuck With Loops in Python - Only Returning First Value
How to Locate the Input Within Div
Check If a Python Script Is Already Running in Windows
Visual Studio Code Intellisense Is Very Slow - Is There Anything I Can Do
Get First Date and Last Date of Current Quarter in Python
Extract File Name from Read_Csv - Python
Auto Reloading Python Flask App Upon Code Changes
How to Increase the Font Size of the Markdown Table in Jupyter Notebook
How to Replace Negative Numbers in Pandas Data Frame by Zero
Heroku: No Default Language Could Be Detected for This App
Calculate Rgb Value for a Range of Values to Create Heat Map