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)+$
Related
Doing URL rewrites in IIS8, and need help with a regular expression.
In a URL like this:
mysite.com/wichita-2014-Honda-Ridgeline
The following expression works great
^wichita-([^/-]+)-([^/-]+)-([^/-]+)$
That splits down to an easy
/index.php?year={R:0}&make={R:1}&model={R:2}
Works great. It breaks on
mysite.com/wichita-2014-Honda-CR-V
Note the new dash in "CR-V". The solution so far has been to have an exception rule preceeding the more general rule:
^wichita-([^/-]+)-([^/-]+)-CR-V$
I've tried
^wichita-([^/-]+)-([^/-]+)-[a-zA-Z0-9\-]*$
But that returns three matches and R:0 is the whole URL! Obviously, that's not what I want.
I have to imagine there's a better way. I just haven't found it yet, and I'm not strong with regular expressions.
So, to sum up: if possible, I'm looking for a regular expression that:
Matches on three terms: year, make, model (just to give them names)
Those three search terms are separated by a dash.
Term 3 might ALSO contain a dash (or a space, expressed in the URL as a plus sign), which should be taken as part of the term and not a separator.
Help please?
I'd use ^wichita-([^/-]+)-([^/-]+)-(.+)$, and map the capture groups R:1, R:2, and R:3 to /index.php?year={R:1}&make={R:2}&model={R:3}
The .+ captures everything after the first two capture groups into the 'anything goes' last bucket.
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.
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
I am learning regular expressions and I am trying to create one that will validation either a whole number or a decimal.
I have created this regular expression:
^(\d+)|([\d+][\.{1}][\d+])$
It almost works, but it says a number like:
12.
12..
12..67
are matches.
I thought
([\d+][\.{1}][\d+])
meant it had to have one or more numbers, followed by a dot (and only one), followed by one or more numbers.
Can someone explain what I am doing wrong?
As a learning process I'm interested in what I am doing wrong rather than what is another way of doing it. I tried following the syntax examples but I have missed something.
You are wrong
([\d+][\.{1}][\d+])
with the square brackets are you creating character classes. that means
[\d+] does mean match a digit or a + once.
[\.{1}] does mean match a . or a { or a 1 or a }
To get the behaviour you expect remove the square brackets
(\d+\.{1}\d+)
This will match at least one digit, a . followed by one or more digits
The other problem here is the ^ belongs only to the first part of your expression and the $ belong only to the last part of your alternation. So you should put brackets around the complete alternation
^((\d+)|(\d+\.{1}\d+))$
If you don't need the match in a capturing group you can remove the brackets around the single alternatives
^(\d+|\d+\.{1}\d+)$
As last point as Jens noted
{1} is redundant \.{1} is the same than \.
Then we are at
^(\d+|\d+\.\d+)$
You can try with:
^(\d+(\.\d+)?)$
Your regex is nearly there, you just need to remove the square brackets -
^(\d+)|(\d+\.{1}\d+)$
Should work for what you want.
I am trying to make what I think is a simple regex for use with mod_rewrite.
I've tried various expressions, many of which I thought were promising, but all of which ultimately failed for one reason or another. They all also seem to fail once I add start/end string delimiters.
For example, ^user/(\d{1,10})(?=/)$ was one I tried, but among other things, it seems to group the trailing slash, and I only want to group the digits. I think I need to use a positive lookbehind, but I'm having difficulty because it's looking behind at a group.
What I am trying to match is strings that 1) begin with "user/" and 2) possibly end with (\d{1,10})/ (1 to 10 digits followed by a single slash)
Should Match:
user/
user/123/
user/1234567890/
Should not match:
user
user//
user/-4/
user/35.5/
user/123
user/123//
user/123/5/
user/12345678901/
Edit: Sorry about the formatting; I do not understand how to format anything via this markdown. Those examples are preceded by 4 spaces which I thought should make a code block, but obviously I thought wrong.
^user/(?:([0-9]{1,10})/)?$ should work just fine.
This: ^user(?=/)(/\d{1,10})?/$ Edit: if you want to group digits, ^user(?=/)(?:/(\d{1,10}))?/$