I have the following regular expression that isn't working the way I thought it would.
("^\\d{2}(?:\\d{2})?\\.\\d{2}(\\.\\d{2-4})?$");
I am trying to match a string that starts with either 2 or 4 digits, followed by a period, followed by 2 digits and then optionally another period and either 2 or 4 digits.
I would expect 33.44.4444 to work, as would 33.33 but anytime I have a string that has a 2nd period, my expression fails.
What am I doing wrong ?
Your regex is correct for what you want to do except for the {2-4} part, if you use {2,4} it will go for the 2 to 4 characters capture you're looking for.
("^\\d{2}(?:\\d{2})?\\.\\d{2}(\\.\\d{2,4})?$");
Hope it helps.
As others have pointed out the syntax {2-4} is incorrect. Use {2,4} to specify a range of occurrences. But also if you only want 2 or 4 (not 3) I would use this regex:
#"^(\d{2}|\d{4})\.\d{2}(\.(\d{2}|\d{4}))?$"
The way you expressed "either two or four digits" in the first section of your expression is correct:
\\d{2}(?:\\d{2})?
The second part does it incorrectly:
(\\.\\d{2-4})?
Copy the first part into the second to fix the problem:
("^\\d{2}(?:\\d{2})?\\.\\d{2}(\\.\\d{2}(?:\\d{2})?)?$");
Demo.
You can use this regex:
^\d{2}(?:\d{2})?\.\d{2}(?:\.\d{2}(?:\d{2})?)?$
\d{2-4} will match {2-4} text literally.
RegEx Demo
Related
I have a regex
/^([a-zA-Z0-9]+)$/
this just allows only alphanumerics but also if I insert only number(s) or only character(s) then also it accepts it. I want it to work like the field should accept only alphanumeric values but the value must contain at least both 1 character and 1 number.
Why not first apply the whole test, and then add individual tests for characters and numbers? Anyway, if you want to do it all in one regexp, use positive lookahead:
/^(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9]+)$/
This RE will do:
/^(?:[0-9]+[a-z]|[a-z]+[0-9])[a-z0-9]*$/i
Explanation of RE:
Match either of the following:
At least one number, then one letter or
At least one letter, then one number plus
Any remaining numbers and letters
(?:...) creates an unreferenced group
/i is the ignore-case flag, so that a-z == a-zA-Z.
I can see that other responders have given you a complete solution. Problem with regexes is that they can be difficult to maintain/understand.
An easier solution would be to retain your existing regex, then create two new regexes to test for your "at least one alphabetic" and "at least one numeric".
So, test for this :-
/^([a-zA-Z0-9]+)$/
Then this :-
/\d/
Then this :-
/[A-Z]/i
If your string passes all three regexes, you have the answer you need.
The accepted answers is not worked as it is not allow to enter special characters.
Its worked perfect for me.
^(?=.*[0-9])(?=.*[a-zA-Z])(?=\S+$).{6,20}$
one digit must
one character must (lower or upper)
every other things optional
Thank you.
While the accepted answer is correct, I find this regex a lot easier to read:
REGEX = "([A-Za-z]+[0-9]|[0-9]+[A-Za-z])[A-Za-z0-9]*"
This solution accepts at least 1 number and at least 1 character:
[^\w\d]*(([0-9]+.*[A-Za-z]+.*)|[A-Za-z]+.*([0-9]+.*))
And an idea with a negative check.
/^(?!\d*$|[a-z]*$)[a-z\d]+$/i
^(?! at start look ahead if string does not
\d*$ contain only digits | or
[a-z]*$ contain only letters
[a-z\d]+$ matches one or more letters or digits until $ end.
Have a look at this regex101 demo
(the i flag turns on caseless matching: a-z matches a-zA-Z)
Maybe a bit late, but this is my RE:
/^(\w*(\d+[a-zA-Z]|[a-zA-Z]+\d)\w*)+$/
Explanation:
\w* -> 0 or more alphanumeric digits, at the beginning
\d+[a-zA-Z]|[a-zA-Z]+\d -> a digit + a letter OR a letter + a digit
\w* -> 0 or more alphanumeric digits, again
I hope it was understandable
What about simply:
/[0-9][a-zA-Z]|[a-zA-Z][0-9]/
Worked like a charm for me...
Edit following comments:
Well, some shortsighting of my own late at night: apologies for the inconvenience...
The - incomplete - underlying idea was that only one "transition" from a digit to an alpha or from an alpha to a digit was needed somewhere to answer the question.
But next regex should do the job for a string only comprised of alphanumeric characters:
/^[0-9a-zA-Z]*([0-9][a-zA-Z]|[a-zA-Z][0-9])[0-9a-zA-Z]*$/
which in Javascript can be furthermore simplified as:
/^[0-9a-z]*([0-9][a-z]|[a-z][0-9])[0-9a-z]*$/i
In IMHO it's more straigthforward to read and understand than some other answers (no backtraking and the like).
Hope this helps.
If you need the digit to be at the end of any word, this worked for me:
/\b([a-zA-Z]+[0-9]+)\b/g
\b word boundary
[a-zA-Z] any letter
[0-9] any number
"+" unlimited search (show all results)
I have the following simple negative look behind
(?<![Ø]\s*)
And the following expression to match an integer or a decimal whether with or without integer part
([0-9]*(?:[.,][0-9]+)?)
the second expression matches 8 8.8 8,8 .8 ,88 etc..
I am trying to combine the 2 expressions to ignore the whole match of the second expression in case its preceded by Ø, so I did
(?<![Ø]\s*)([0-9]*(?:[.,][0-9]+)?)
and those values for testing
88.88
88,88
,88
.5
Ø .8
Ø 8.8
First 4 values match as expected but a part of the last 2 gets partially matched and I expected it to not match at all, can someone please tell what i am missing?
You can try this
(?<![Ø]\s*|[.,\d])(?=[\d.,]{1,})([0-9]*(?:[.,][0-9]+)?)
^^
||
regexstorm demo
A bit simpler version suggested by bobble bubble
(?<!Ø\s*|[.,\d])(\d*[.,]?\d+)
Always better to explain answers on SO. So here we go: the problem is that the expression can actually match anywhere in the string. Thus, if the test case has more than one character in the match it might fail on, then a match will start one character in and match the rest. Even more so given that the expression given can match some blank strings. The best way of doing things would be to:
Add a check to make sure that the match has at least one digit, and
Add a check to make sure that it is at the start of a potential match.
Both of these can be done with the negative lookbehind. Thus: (?<!Ø\s*[.,\d]*)\d*[.,]?\d+
I would like to find the match for:
\024jack3hall2\c$
\024jack3hall02\c$
\024jack3hall12\c$
but not for:
\024jack3hall023\c$
difference is the number of digits in the end part. I would like to have only 1 or 2, not 3.
my try:
\\\\024[a-zA-Z0-9]+[0-9]{1,2}\\[a-zA-Z]{1}\$(?!.)
I tried only on http://regexr.com/ but will implement in C#.
Is it possible to edit my try or I have to write several separate checks?
Why is
{1,2}
not working? \024jack3hall12343\c$ is also matching,
From the examples you have shown, something as simple as:
[^\d](\d{1,2})\\
Should work. It will match 1 or 2 digits followed by a \ so long as it isn't proceeded by another digit.
The matched digits are in a capture group if you need them (or you can just remove the brackets if you don't need that).
As for your original effort, right here:
\\\\024[a-zA-Z0-9]+[0-9]{1,2}
You are matching 1 or more from the range a-z, A-Z or 0-9. So that will match your extra digits if they come at the end of that pattern.
Answer:
\\\\024[a-zA-Z0-9]+[^\d](\d{1,2})\\[a-zA-Z]{1}\$(?!.)
I believe you were not escaping backslash properly.
Here is the correct regex:
\\024[a-zA-Z0-9]+[0-9]{1,2}\\[a-zA-Z]{1}\$(?!.)
I need a regular expression that will match one or two digits before the dot and one digit after the dot (0 or 5).
I tried it out for at least one hour, and I'm getting mad....
Possible results should be:
5,0
5,5
30,0
30,5 etc.
If just one digit is insert, it want a result as follows :
5 --> 5,0
Is there someone who can help me? Thanks a lot!!
You just want to check for one or two digits, followed by the dot literal, and either '0' or '5'.
^\d{1,2}\.[05]$
That doesn't handle the single digit one, though. There's not an easy way to just match a single digit in the same regex you're matching one or two, so you could use a second regex:
^\d$
Then convert that to a double/float if you get any matches.
Try the following regex...
(?:\d{1,2}(?=.)|(?<=.)[50])
string regex=#"(?<b>\d{1,2})(?<a>[.]0|[.]5)?";
Match m=Regex.Match(input,regex);
string result=m.Groups["b"].Value+","+m.Groups["a"].Value==""?0:m.Groups["a"].Value;
Above code would give these results for input
550.57 => 50,5
644 => 44,0
I'd like to be able to match a specific sequence of characters, starting with a particular substring and ending with a particular substring. My positive lookahead regex works if there is only one instance to match on a line, but not if there should be multiple matches on a line. I understand this is because (.+) captures up everything until the last positive lookahead expression is found. It'd be nice if it would capture everything until the first expression is found.
Here is my regex attempt:
##FOO\[(.*)(?=~~)~~(.*)(?=\]##)\]##
Sample input:
##FOO[abc~~hi]## ##FOO[def~~hey]##
Desired output: 2 matches, with 2 matching groups each (abc, hi) and (def, hey).
Actual output: 1 match with 2 groups (abc~~hi]## ##FOO[def, hey)
Is there a way to get the desired output?
Thanks in advance!
Use the question mark, it will match as few times as possible.
##FOO\[(.*?)(?=~~)~~(.*?)(?=\]##)\]##
This one also works but is not as strict although easier to read
##FOO\[(.*?)~~(.*?)\]##
The * operator is greedy by default, meaning it eats up as much of the string as possible while still leaving enough to match the remaining regex. You can make it not greedy by appending a ? to it. Make sure to read about the differences at the link.
You could use the String.IndexOf() method instead to find the first occurrence of your substring.