I am trying to write regex so that it returns me true for all the below possibilities
1.2E3
12.22e32
+1.2
1,222
3,222
+3,222E23
3.2E2,333
A number is valid with comma if after comma I have 3 digits.
I have the regex which returns probably works well for E/e and decimal point
[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?
I also have below regex which will work well for commas.
^(\d?\d?\d(,\d\d\d)*|\d+)(\.\d\d)?$
First thing for above regex , I do not understand how it works. I know ^ is used to negate an expression. I understand the rest of the part but why it has to negate it?
I have made a few tries to combine the 2 to get my job done.
Here are my tries(they do not work for commas)
"[-+]?[0-9]*(,[0-9][0-9][0-9])*\\.?[0-9]+([eE][-+]?[0-9]+)?"
"/[1-9](?:\\d{0,2})(?:,\\d{3})*(?:\\.\\d*[1-9]+([eE][-+]?[0-9]+)?)?|0?\\.\\d*[1-9]+([eE][-+]?[0-9]+)?|0/"
Can somebody help me out with this. This seems to be giving me headache
This works:
/^[+-]?[\d]{1,3}((\.[\d]{1,3})?|(,[\d]{3})*)([eE][\d]{1,3}(,[\d]{3})*)?$/
Mind you, there will be more elegant regex for this.
Cheers.
I am assuming that you are not allowing numbers without commas. That is 123456 is not valid unless it's written as 123,456. Given that assumption, this regex will work:
^[+-]?\d{1,3}((\.\d{1,3})?|(,\d{3})*)([eE]\d{1,3}(,\d{3})*)?$
You can see the details of how it works on debuggex.
Related
I am trying to learn regex for my project I am working on to use it for input validation because I have been told that using regex is one of the best ways to do input validation.
So, I am trying to make sure a string contains an unknown number of coordinates which are separated by a space. An example of what the input will look like is 2,2 23.45,6 45,21.65 2,2 I'm not sure if it matters, but the last coordinate will always match the first. There can't be any symbols or extraneous spaces or commas. Only a decimal numbers split with a comma, followed by a space or an endline character.
I realize that this is probably quite a complex expression, and I am pretty much jumping into regex blind, so any help with this would be really appreciated. I am programming in c++ if that changes anything. Thanks.
EDIT:
I had forgotten about the possibility of negative numbers and newline characters. I also accept negative numbers and newline characters in the input.
So the input -2.3,2 34,-2 -2.3,2\n is acceptable. Thank you to everyone so far for the help.
You can try this regex:
^\d+(?:\.\d+)?,(?:\d+(?:\.\d+)? \d+(?:\.\d+)?,)*\d+(?:\.\d+)?$
Demo here:
Regex101
Note: The regex \d+(?:\.\d+)? matches any number, possibly having a decimal component. The ?: inside the parenthesis marks the quantity as a non capture group. This tells the regex engine to not capture what is inside, as we don't want to actually capture anything here. This possibly will result in a more efficient regex.
So I wanted to limit a textbox which contains an apartment number which is optional.
Here is the regex in question:
([0-9]{1,4}[A-Z]?)|([A-Z])|(^$)
Simple enough eh?
I'm using these tools to test my regex:
Regex Analyzer
Regex Validator
Here are the expected results:
Valid
"1234A"
"Z"
"(Empty string)"
Invalid
"A1234"
"fhfdsahds527523832dvhsfdg"
Obviously if I'm here, the invalid ones are accepted by the regex. The goal of this regex is accept either 1 to 4 numbers with an optional letter, or a single letter or an empty string.
I just can't seem to figure out what's not working, I mean it is a simple enough regex we have here. I'm probably missing something as I'm not very good with regexes, but this syntax seems ok to my eyes. Hopefully someone here can point to my error.
Thanks for all help, it is greatly appreciated.
You need to use the ^ and $ anchors for your first two options as well. Also you can include the second option into the first one (which immediately matches the third variant as well):
^[0-9]{0,4}[A-Z]?$
Without the anchors your regular expression matches because it will just pick a single letter from anywhere within your string.
Depending on the language, you can also use a negative look ahead.
^[0-9]{0,4}[A-Za-z](?!.*[0-9])
Breakdown:
^[0-9]{0,4} = This look for any number 0 through 4 times at the beginning of the string
[A-Za-z] = This look for any characters (Both cases)
(?!.*[0-9]) = This will only allow the letters if there are no numbers anywhere after the letter.
I haven't quite figured out how to validate against a null character, but that might be easier done using tools from whatever language you are using. Something along this logic:
if String Doesn't equal $null Then check the Rexex
Something along those lines, just adjusted for however you would do it in your language.
I used RegEx Skinner to validate the answers.
Edit: Fixed error from comments
As a preface, I realize there are other topics on regular expressions with comma separated numbers, but when I tried to use those solutions, they didn't work.
Basically, I am trying to create a regular expression to recognize comma separated numbers (in this case without spaces). Before trying to convert this into actual regex syntax, I realize that it should probably work something like this, where 'd' is a number and ',' is a comma, and '+' is a kleene plus:
((d+),)*(d+)
or
(d+)(,(d+))*
Here's the code I am using in an Apex validation to make sure that a certain field is a list of numbers separated by commas without spaces (note: I have tried several variations of this to no avail, but will only post one):
(\d+,)*(\d+)
For some reason this isn't working, but it seems to be the correct syntax of any digit 1 or more times followed by a single comma, and that entire expression can be repeated 0 or more times, and that entire repeated expression should always be followed by at least 1 digit.
This expression in practice does recognize all the accepted forms (ex: 100 or 100,200 etc.), but for some reason it also accepts answers like
'100,200,'
or
'100,200,,'
or
'100,,200'
I'm pretty stumped as to why this won't work as well as the previously given solutions which seem to do the same thing mine do. Thanks for any help in advance!
That's it:
^(\d+,)*\d+$
The anchors ^$ will make the difference because they will force the whole string (not just a part) to match the pattern
You should try pattern like this:
^(?:(\d+),)+(\d)+$
I am trying to learn regular expressions and was hoping someone could help me out. WOuld appreciate if someone can help me come up with a regular expression to validate that an input must be of the form
Graph: XY5, YZ4, ST7
Each part such as XY5 represents an edge in the graph and the number represents a the edge weight. There can be any number of such edges.
This is what I have till now. It's probably not correct
"^Graph:\\s{1}[A-ZA-Z\\d,\\s]+"
This might be what you're looking for:
/^Graph: (?:[A-Z]{2}\d(?:$|, ?))+/
See it here in action: http://regexr.com?309av
Here's an explanation of what the regex does (screenshot from RegexBuddy, which is probably the best tool for you if you're trying to learn Regular Expressions):
Try this
/^Graph:(\s+[A-Z][A-Z]\d+)+$/
You should explain your input format a little better. This might do it, from the single example I have and what you said. It does not allow a graph to be empty, which may or may not be part of your requirements.
"^Graph:(\s\w{2}\d+,?)+"
to explain:
^Graph: will cover the start of the line
(\s\w{2}\d+,?)+
\s is a space
\w{2} matches exactly 2 alphanumeric characters (hint: you could make this better!)
\d+ matches 1 or more digits, since I am assuming an edge can have a two digit length ( such as 10)
,? matches a comma optionally. (hint: you could make this better as well, as it will not necessitate a comma between each entry!, maybe by using an or and the end of string delimiter!)
I purposely left some room for improvement, because if you think of some of it on your own, you will accomplish your goal of becoming better with regular expressions.
i have following regular expression but it's not working properly it takes only three values after # sign but i want it to be any number length
"/^[a-zA-Z0-9_\.\-]+\#([a-zA-Z0-9\-]+\.)+[a-zA-Z0-9]{2,4}$/"
this#thi This is validated
this#this It is not validating this expression
Can you please tell me what's the problem with the expression...
Thanks
If you want your regex to match "any number length" then why are you using {2,4}?
I think a better example of the strings you're trying to match might give others a better idea of what you want, because based on your regex it is a bit confusing what you're looking for.
Try this:
^[a-zA-Z0-9_.-]+#([a-zA-Z0-9-]+\.)+[a-zA-Z0-9]{2,4}$
The main problem is that you didn't escape the dot: \.. In regular expression the dot matches everything (mostly), making your regex quite liberal.