regex allow only numbers or empty string - regex

Can someone help me create this regex. I need it to check to see if the string is either entirely whitespace(empty) or if it only contains positive whole numbers. If anything else it fails. This is what I have so far.
/^\s*|[0-9][0-9]*/

You're looking for:
/^(\s*|\d+)$/
If you want a positive number without leading zeros, use [1-9][0-9]*
If you don't care about whitespaces around the number, you can also try:
/^\s*\d*\s*$/
Note that you don't want to allow partial matching, for example 123abc, so you need the start and end anchors: ^...$.
Your regex has a common mistake: ^\s*|\d+$, for example, does not enforce a whole match, as is it the same as (^\s*)|(\d+$), reading, Spaces at the start, or digits at the end.

To match a number or empty string '' i.e the user has not entered any input do this
(^[0-9]+$|^$)
To match a number, an empty string, or a white space character
(^[0-9]+$|^$|^\s$)
Test this on regex101

Kobi has a good answer but technically you don't have to capture it (unless you're going to do something the output)
/^[\s\d]+$/
Or if you don't care if the string is completely empty (i.e. "")
/^[\s\d]*$/
To clarify I understood the original question to mean whitespace in the string should be ignored.

You can try it-
/^\d*$/
To match with white space-
/^[\s\d\s]*$/

This helped me. It matches empty string and any number you enter.
/^(|\d)+$/
Try here if you want: regex101.com

^\d+([\.\,][0]{2})?$
I found this worked for me. Allow any whole number, but only a decimal of .00
Pass
999 90
100
100.00
101.00
1000.00
Fails
101.01
1000.99
Try it at http://regexpal.com/

Related

Regex match not validating correctly

I have the following regex ^(2[0-3]|1?[0-9])?(\:[0-5]?[0-9])?$
8:45 matches, however 08:45 does not.
How can this be re-written to also allow 08:45?
I would also like like to check for either the . or : if possible instead of just the .
As well as "1", also allow "0" at the start of the hours before "20:00":
^(2[0-3]|[01]?[0-9])?(:[0-5]?[0-9])?$
Note that you don't need to escape colons - they have no speacial regex meaning.
Also, since everything is optional, this regex matches a blank string.
If you want to match something, add (?!$) after ^. ^(?!$) is a negative look ahead anchored to start that requires that end of input does not follow the start of input (ie, the input is not zero length).
Please try this regEx.
[0]?\d[:\.]\d\d
which can capture 8:45 08:45 8.45 and 08.45
or click this to see the result.
https://regex101.com/r/tLEqhH/1

How to allow only WhatsApp format numbers in a regex?

so I'm trying to make this Regex allow this the Dash symbol - For Example this Phone Number is not matching right now
+212 659-123456
So I need someone to help me change the Regex to allow it
please Here is the Regex:
^\+(?:[0-9]\x20?){6,14}[0-9]$
Because I am trying to only accept the format that is used by WhatsApp and some numbers might have multiple spaces or multiple Dashes. Also the Plus sign has to be mandatory Here some more examples of the format on WA.
+96274567123
+967773-123-123
+212 627-024321
+212689-881234
+966 54 666 4373
The numbers above cover 99% of the cases. I would appreciate any help, thanks and regards
I would just use:
^(?=(?:[+ -]*[0-9][+ -]*){11,12}$)\+(?:[0-9]+[ -]?)+[0-9]$
Explanation:
(?=(?:[+ -]*[0-9][+ -]*){11,12}$) Positive lookahead which checks that the string has exactly 11 or 12 digits in it.
\+(?:[0-9]+[ -]?)+[0-9] Has to start with a + and end with a digit, in between can be groups of one ore more digits plus optionally a single or -.
regex101 demo
^\+([\s\-0-9]){6,14}$
This would catch all your entries. It would be easier if you delete all whitespaces and unwanted characters and test than. Especially when the String to test becomes longer and longer because of whitespaces.

Get last characters up to specific character

Lets say I have a string something-123.
I need to get last 5 (or less) characters of it but only up to - if there is one in the string, so the result would be like thing, but if string has no - in it, like something123 then the result would be ng123, and if string is like 123 then the result would be 123.
I know how to mach last 5 characters:
/.{5}$/
I know how to mach everything up to first -:
/[^-]*/
But I can not figure out how to combine them, and to make things worse I need to get the match without extracting it from specific groups and similar advanced regex stuff because I want to use it in SQL Anywhere, please help.
Tank you all for the help, but looks like a complete regex solution is going to be too complicated for my problem, so I did it very simple: SELECT right(regexp_substr('something-123', '[^-]*'), 4).
One option is to group the result:
(.{4})-
Now you have captured the result but without the -.
Or using lookarounds you can:
.{4}(?=-)
which matches any 4 characters that appears before "-".
You can use:
.{5}(?=(?:-[^-]*)?$)
See the regex demo
We match 5 symbols other than a newline only before the last - in the string or at the very end of the string ((?=(?:-[^-]*)?$)). You only need to collect the matches, no need checking groups/submatches.
UPDATE
To match any 1 to 5 characters other than a hyphen before the first hyphen (if present in the string), you can use
([^-]{1,5})(?:(?:-[^-]*)*)?$
See demo. We rely on a lookahead here, that checks if there are -+non-hyphen sequences are after the expected substring.
An faster alternative:
^[^-]*?([^-]{1,5})(?:-|$)
This regex will search for any characters other than - up to 1 to 5 such characters.
Note that here, the value we need is in Group 1.
How about:
(.{5})(?:-[^-]+)?$
The result is in group 1
Try this regex:
(.{1,5})(?:-.*|$)
Group 1 has the result you need
demo

How to extract a numeric substring from a string but only if the previous string part matches a target

So I am trying to extract defect numbers from changeset comments in TFS. However, there are several ways people have entered the numbers:
"Defect 1321: blah blah blah"
"Fixes HPQC 1427. Logic modified"
"- Bug 976 - Customer"
I am not great with regexes so any help would be great. I prepare the string ahead of time by tolowering it and stripping out the # and ., so I can be assured I am looking for something that starts with (defect|hpqc|bug) has an optional space (\s) then a number (\d) then ends with a space (\s) but this didn't work:
(defect|hpqc|bug)\s\d\s
I only want to find the first match.
I want to extract the numeric component but only if the previous word is a match.
I am sure this is a result of my trivial knowledge of regex creation.
Case matters (usually) and you want more than one digit \d+ and there is an optional number sign too so something like this should work, depending on your system:
(Defect|HPQC|Bug)\s*#?\s*(\d+)
This allows spaces and # or neither before the digits, and captures the digits. It would help to know if you are using python or something else (tag your question).
I believe this regex should work for you:
(?:defect|hpqc|bug)\s+(\d+)\s+
Defect/Bug # is available in matched group #1
If you are looking only for the number after the keyword here is a regex might should help...
(?<=(Defect|HPQC|Bug)\s*#?\s*)\d+
Good Luck!
I precise Beroe response :
(?:Defect|HPQC|Bug)\s*\#?\s*(\d+)`
(?:Defect|HPQC|Bug) : detect but don't capture
\# : slash for disable the comment
It works for me on Expresso

TextMate: Regex replacing $1 with following 0

I'm trying to fix a file full of 1- and 2-digit numbers to make them all 2 digits long.
The file is of the form:
10,5,2
2,4,5
7,7,12
...
I've managed to match the problem numbers with:
(^|,)(\d)(,|$)
All I want to do now is replace the offending string with:
${1}0$2$3
but TextMate gives me:
10${1}05,2
Any ideas?
Thanks in advance,
Ross
According to this, TextMate supports word boundary anchors, so you could also search for \b\d\b and replace all with 0$0. (Thanks to Peter Boughton for the suggestion!)
This has the advantage of catching all the numbers in one go - your solution will have to be applied at least twice because the regex engine has already consumed the comma before the next number after a successful replace.
Note: Tim's solution is simpler and solves this problem, but I'll leave this here for reference, in case someone has a similar but more complex problem, which using lookarounds can support.
A simpler way than your expression is to replace:
(?<!\d)\d(?!\d)
With:
0$0
Which is "replace all single digits with 0 then itself".
The regex is:
Negative lookbehind to not find a digit (?<!\d)
A single digit: \d
Negative lookahead to not find a digit (?!\d)
Single this is a positional match (not a character match), it caters for both comma and start/end positions.
The $0 part says "entire match" - since the lookbehind/ahead match positions, this will contain the single digit that was matched.
To anyone coming here, as #Amarghosh suggested, it's a bug, or intentional behavior that leads to problems if nothing else.
I just had this problem and had to use the following workaround: If you set up another capture group, and then use a conditional insertion, it will work. For example, I had a string like <WebObject name=Frage01 and wanted to replace the 01 with 02, so I captured the main string in $1 and the end number in $2, which gave me a regex that looked like (<WebObject name=(Frage|Antwort))(01).
Then the replace was $1(?2:02).
The (?2:02) is the conditional insertion, and in this instance will always find something, but it was necessary in order to work around the odd conundrum of appending a number to the end of $n. Hope that helps someone. There is documentation on the conditional insertion here
In TextMate 1.5.11 (1635) ${1} does not work (like the OP described).
I appreciate the many suggestions re altering the query string, however there is a much simpler solution, if you want to break between a capture group and a number: \u.
It is a TextMate specific replacement syntax, that converts the following character to uppercase. As there is no uppercase for numbers, it does nothing and moves on. It is described in the link from Tim Pietzcker's answer.
In my case I had to clean up a csv file, where box measurements were given in cm x cm x mm. Thus I had to add a zero to the first two numbers.
Text: "80 x 40 x 5 mm"
Desired text: "800 x 400 x 5 mm"
Find: (\d+) x (\d+) x (\d+)
Replace: $1\u0 x $2\u0 x $3 mm
Regarding the support of more than 10 capture groups, I do not know if this is a bug. But as OP and #rossmcf wrote, $10 is replaced with null.
You need not ${1} - replace strings support only up to nine groups maximum - so it won't mistake it for $10.
Replace with $10$2$3