this is my regex for digital dnumbers:
\d+(.\d+)+(,\d+)
but now i have problem that number 3 or 30 are not valid any more. What must be my regex that also number 3 and 40 will pass.
Thx
\d+(\.\d+)*(,\d+)?
The + in regex means "at least one", whereas the * means "zero or more" and the ? means "either one or none".
Also, you have to escape periods as \. since otherwise the . character is a special character in regex meaning "any single character".
If you want to make sure that the .'s in the number (if present) always separate digits by groups of 3, you could use this (the {x} syntax means "exactly x repetitions"):
\d+(\.\d{3})*(,\d+)?
Or to force thousands separators all the time, you could use this (the {x,y} syntax means "anywhere from x to y repetitions):
\d{1,3}(\.\d{3})*(,\d+)?
\d+((\.\d+)|(,\d+))?
so you want a regex that matches 1 and 3.000 and 3.000,5 ?
If you don't want to capture the result this should do:
[.\d]+(,\d+)?
but keep in mind that this is not very accurat anyway since it also matches 2.0.0,12 and you should also include a plus minus check:
^(\+|-)?[.\d]+(,\d+)?
In C# you could do better with
double result;
bool isDouble = Double.TryParse("3.000,5", Globalisation.CultureInfo.InvariantCulture);
If what you really want is . for thousands separator, and , for the decimal separator, try this:
\d{1,3}(\.\d{3})*(,\d+)?
Related
I have this regex to allow for only alphanumeric characters.
How can I check that the string at least contains 3 alphabet characters as well.
My current regex,
if(!/^[a-zA-Z0-9]+$/.test(val))
I want to enforce the string to make sure there is at least 3 consecutive alphabet characters as well so;
111 // false
aaa1 // true
11a // false
bbc // true
1a1aa // false
+ means "1 or more occurrences."
{3} means "3 occurrences."
{3,} means "3 or more occurrences."
+ can also be written as {1,}.
* can also be written as {0,}.
To enforce three alphabet characters anywhere,
/(.*[a-z]){3}/i
should be sufficient.
Edit. Ah, you'ved edited your question to say the three alphabet characters must be consecutive. I also see that you may want to enforce that all characters should match one of your "accepted" characters. Then, a lookahead may be the cleanest solution:
/^(?.*[a-z]{3})[a-z0-9]+$/i
Note that I am using the case-insensitive modifier /i in order to avoid having to write a-zA-Z.
Alternative. You can read more about lookaround assertions here. But it may be a little bit over your head at this stage. Here's an alternative that you may find easier to break down in terms of what you already know:
/^([a-z0-9]*[a-z]){3}[a-z0-9]*$/i
This should do the work:
^([0-9]*[a-zA-Z]){3,}[0-9]*$
It checks for at least 3 "Zero-or-more numerics + 1 Alpha" sequences + Zero-or-more numerics.
You want to match zero or more digits then 3 consecutive letters then any other number of digits?
/\d*(?:[a-zA-Z]){3,}\d*/
This is vanilla JS you guys can use. My problem is solved using this.
const str = "abcdggfhf";
const pattern = "fhf";
if(pattern.length>2) {
console.log(str.search(pattern));
}
I was trying to create a regular expression to find repeated strings of digits.
eg:
1 -not matching
11 -matching
122 -matching
1234 -not matching
what i used is \d+. Tutorial are telling
the "+" is similar to "*", except it requires at least one repetition.
But when i tried it is matching with any number. Any idea why?
Update
The tutorial i tried : http://www.codeproject.com/Articles/9099/The-Minute-Regex-Tutorial
The repetition constructs in Regular Expressions, +, *, {x}, do not repeat "what you found the first time around", they repeat "the pattern that finds things".
So this:
\d+
Will not find one digit, then match a sequence of that digit, instead it will first find one digit, then try to find another digit, then another, etc.
If you want it to repeat "what it found" you have to explicitly say so:
(\d)\1+
The \1 here says "I will match whatever is in the first group again", this regular expression should match sequences of the same digit, instead of sequences of digits.
^\d*(\d)\1+\d*$
You can use this.See demo.\d+ would match any intergers 1 or more time.You need to use \1 to find repeated digits.
https://regex101.com/r/hI0qP0/4
It works properly. \d+ is not a repetition of a specific digit, it is a repetition of one or more \d. \d+ will match 1 (one or more digit), 12 (one or more digit), 122 (one or more digit)... you see the idea. If you want to see two or more repetitions, you'd need to say \d\d+ or \d{2,} - but this, too, says that you want two or more digits, not two or more of a same digit. To say that, you need backreferences: (\d)\1+ is two or more of a same digit: a digit we remember, then one or more of that remembered thing.
I need a regular expression validation expression that will
ALLOW
positive number(0-9)
, and .
DISALLOW
letter(a-z)
any other letter or symbol except . and ,
for example, on my asp.net text box, if I type anything#!#--, the regular expression validation will disallow it, if I type 10.000,50 or 10,000.50 it should allowed.
I've been trying to use this regex:
^\d+(\.\d\d)?$
but my textbox also must allow , symbol and I tried using only integer regex validation, it did disallow if I type string, but it also disallow . and , symbol while it should allow number(0-9) and also . and , symbol
Don't Use \d to match [0-9] in .NET
First off, in .NET, \d will match any digits in any script, such as:
654۳۲١८৮੪૯୫୬१७੩௮௫౫೮൬൪๘໒໕២៧៦᠖
So you really want to be using [0-9]
Incomplete Spec
You say you want to only allow "digits, commas and periods", but I don't think that's the whole spec. That would be ^[0-9,.]+$, and that would match
...,,,
See demo.
Tweaking the Spec
It's hard to guess what you really want to allow: would 10,1,1,1 be acceptable?
We could start with something like this, to get some fairly well-formed strings:
^(?:[0-9]+(?:[.,][0-9]+)?|[1-9][0-9]{0,2}(?:(?:\.[0-9]{3})*|(?:,[0-9]{3})*)(?:\.[0-9]+)?)$
Play with the demo, see what should and shouldn't match... When you are sure about the final spec, we can tweak the regex.
Sample Matches:
0
12
12.123
12,12
12,123,123
12,123,123.12456
12.125.457.22
Sample Non-Matches:
12,
123.
1,1,1,1
Your regex would be,
(?:\d|[,\.])+
OR
^(?:\d|[,\.])+$
It matches one or more numbers or , or . one or more times.
DEMO
Maybe you can use this one (starts with digit, ends with digit):
(\d+[\,\.])*\d+
If you need more sophisticated price Regex you should use:
(?:(?:[1-9]\d?\d?([ \,\.]?\d{3})*)|0)(?:[\.\,]\d+)?
Edit: To make it more reliable (and dont get 00.50) you can add starting and ending symbol check:
(^|\s)(?:(?:[1-9]\d?\d?([ \,\.]?\d{3})*)|0)(?:[\.\,]\d+)($|\s)?
I think the best regex for your condition will be :
^[\d]+(?:,\d+)*(?:\.\d+)?$
this will validate whatever you like
and at the same time:
not validate:
numbers ending in ,
numbers ending in .
numbers having . before comma
numbers having more than one decimal points
check out the demo here : http://regex101.com/r/zI0mJ4
Your format is a bit strange as it is not a standard format.
My first thought was to put a float instead of a string and put a Range validation attribute to avoid negative number.
But because of formatting, not sure it would work.
Another way is the regex, of course.
The one you propose means :
"some numbers then possibly a group formed by a dot and two numbers exactly".
This is not what you exepected.
Strictly fitted your example of a number lower than 100,000.99 one regex could be :
^[0-9]{1-2}[\.,][0-9]{3}([\.,][0-9]{1-2})?$
A more global regex, that accept all positive numbers is the one posted by Avinash Raj : (?:\d|[,\.])+
I am trying to make a few regex strings to use in my syntax highlighter, this if the first time I have ever used them and I am having a deal of difficulty...
The first four are, I will have a specified character followed by any number of numbers, match it.
The best I have is "G[0-9]|G[0-9][0-9]|G[0-9][0-9][0-9]" to match either G#, G##, or G###
but I want to do G with any number of numbers after it.
The next three are, I will have a character (X,Y,Z, or P) and I want to match it if there is no letter or symbol behind it
"[X|Y|Z|P][0-9]"
These next few are harder, match "#11.11=11.11" where 1 is a number and there can be any number of numbers between the pound sign, the periods, and the equal sign. And the periods do not have to be there can also be "#11=11" or " #1.1=11" or "#11=1.1"
I have no idea... "#[0-9][ |.] ..."
Anything after a " ' " and between a newline
'[A-Za-z0-9]\n" but I know this only gives me one character...
And the easy one I think is anything between two () or []
"(*) | [*]"?
Quick and dirty, but tested using regexpal
1) G[0-9]{1-3} - the '{1-3}' specifies the last symbol to occur one to three times.
2) ((.*|)) - you put a '\' before the '(' and ')' as escape characters
3) [0-9]1*(.|)1*=1*(.|)1 - this matches your three examples
4) \'.*\n - I think this should work... '\n' represents a new line char right?
5) ((|[).*()|]) - this one has those escape characters again
Again...quick and dirty. Regexpal.com is your friend
1> G[0-9]{1,3}
2> No, it's WRONG.
The correct one is [XYZ][0-9]
(you do not use an OR operator (|), but just write the characters side by side within square braces)
You should really look up how to use regexes. Having said that:
I will have a specified character followed by any number of numbers, match it
G\d+
I will have a character (X,Y,Z, or P) and I want to match it if there
is no letter or symbol behind it
(?<!\w)[XYZP][0-9]
These next few are harder, make "#11.11=11.11" blue
Huh?
Anything after a " ' " and between a newline
'(.+?)\n
And the easy one I think is anything between two () or []
\(.+?\)|\[.+?\]
And the easy one I think is anything between two () or []
"(*) | [*]"?
#"\([^(]*\)" and #"\[[^\[]*\]"
It means: an open bracket - then any number of characters which are not an open bracket - and a close bracket.
Slashes are required to indicate to the regex engine that brackets should be treated literally.
# - verbatim string - is to inform C#, in turn, that those slashes should be taken literally and not as C# escape characters.
Anything after a " ' " and between a newline
Similarly: #"'[^']*\n"
G\d+
[XYZP](?=\d)
#(\d+(\.\d+)?)=(\d+(\.\d+)?)
'.*?\n
\(.*?\)|\[.*?\]
Regex explanation here.
The first one:
G[0-9]+
In regular expressions + means at least 1 or more (repetitions of the previous "characters").
You could also use * for none or any number of repetitions.
The second might be something like this:
^(X|Y|Z|P)$
This actually matches only if it's at the beginning of a line and has no characters behind. If you want it to be anywhere and only exclude certain characters behind it, modify the following:
[XYZP][^0-9a-z]
This is X or Y or Z or P followed by NOT 0-9 and NOT a-z
Notice that I use the OR character | in the first example in brackets, but not in the square brackets.
For the third one:
#[0-9]+\.[0-9]+=[0-9]+\.[0-9]+
Might not be 100 percent correct, I always confuse when to escape which characters. You might need to escape # and =.
For the last one:
(\(.*\)|\[.*\])
For the first one you can use this Regex :
^G\d+
For G with any number of digits after it
\b([Gg]\d+)\b
This matches a wordboundary (\b) followed by a lower or upper G [Gg], followed by 1 or more (+) digits (\d), followed by a wordboundary (\b)
The next three are, I will have a character (X,Y,Z, or P) and I want
to match it if there is no letter or symbol behind it
This is a little tougher
\b[XYZP]([\W]|_)
This matches an XYZ or P followed by a non-word character \W, (word characters are typically a-z, 0-9 and the underscore), so after saying we don't want a word character, we add in that the _ is allowed.
I use perl for regex, but it should hopefully be the same as what you're looking for.
For the first one, G[0-9]+ should work. The square brackets means that the regex looks for only one of the characters within the brackets (the characters being 0 through 9) and the + right after it means that it looks for one or more matches.
The second is a bit more tricky, but I would use \s[XYPZ]. The square brackets function the same as before, only matching one of X, Y, P or Z. Also the \s matches any whitespace character (tab, space, newline, etc.).
For the third one, I would try #[0-9]+\.?[0-9]+=[0-9]+\.?[0-9]+. If we go from left to right, we encounter \.? and it's new. \. matches a literal period (you have to escape it with the backslash, as just a period by itself means that it can match one of any character). The question mark means that the period can either be there or not (matches zero or one instance of a period).
The fourth one: '.*\n. The combination of the period by itself and the asterisk means that it'll match zero or more characters, the characters being anything at all. I'm not too sure if you need to escape the single quotes though.
And for the fifth one, (\(.*\)|\[.*\]) should do the trick. You need to escape the []() inside the brackets because they mean something by themselves. Also, the | means or, so the regex can either matches whatever is on the left side of the bar, or on the right.
You can specify repetitions in different ways. A star "*" after a term means, repeat the term zero, one or several times. A plus sign "+" means, repeat the term one or several times. You can also specify a number range with {n,m}. In your case the expression would be
G\d{1-3}
where \d is a digit.
With this expression you can match a position that does not preceed a suffix
find(?!suffix)
I am not sure what you mean by symbol
[XYZP](?![a-zA-Z specify your symbols here])
For the pound number
\#\d+(\.\d+)?=\d+(\.\d+)?
\# the pound sign
\d+ at least one digit
(\.\d+)? optionally (?) a period succeeded by at least one digit
finally an equal sign succeeded by another number
Everything between "'" and \n. Use this pattern here, which finds a position between a prefix and a suffix.
(?<=prefix)find(?=suffix)
(?<=').*(?=\n)
.* means any character as many times as possible. Alternatively you could use
(?<=').*?(?=\n)
.* means any character as few times as possible, if too many \n are taken. Also be carefult with the RegexOption.Multiline. Depending on its setting you will have to test for the end of line with $ instead of \n.
For the parentheses () or [] you can use the same pattern again
(?<=prefix)find(?=suffix)
(?<=\().*?(?=\))|(?<=\[).*?(?=])
where | is the alternative.
I am trying to capture a value out of a string. The string's format should be
01+XXXX
and I want to capture XXXX using a regular expression. This is what I came up with -
01+\\s*(?<1>[.0-9]*)
But that won't work. What DOES work is -
01+\\s*(?<1>[+.0-9]*)
The only difference is adding the + into the character class. My main question is - why does the second expression work and the first expression doesn't? In the first one, I look for 01+ and the rest of it should go to [.0-9]. It seems to me that the second one wants to read + twice - is that not what its doing? I am pretty new to regular expressions so I feel like I might be missing something small.
On this site http://www.codeproject.com/Articles/9099/The-30-Minute-Regex-Tutorial it says that + is used for "Repeat one or more times". So is it trying to read 01+ more than once?
It's reading the 1 one or more times. That is, the regex 01+ matches 01 or 011 or 0111 etc.
But it doesn't match the +. If you want to match a literal +, write 01\+ or 01[+] for the regex.
The + is a special character, meaning "one or more times." In this case, it means 01, 011, 0111, etc. instead of 01+. If you want to use it literally, you need to escape it, like this: \+
Note: It looks like you are using it with strings, so you would need to double-escape: \\+
It works inside a character class ([+]) because character classes take most characters literally, with exceptions including \ and ].
'+' is a special character in regex, it means "1 or more times". So what you have written means:
The character '0'
The character '1' one or more times
Whitespace 0 or more times
etc.
If you want to match a literal plus you need to escape it:
01\+\\s*(?<1>[.0-9]*)
The + is a quantifier, as explained in the tutorial you linked. So, your regex means "match a zero, then one or more ones, then zero or more whitespaces, then ...".
The plus needs to be escaped:
01\\+\\s*(?<1>[.0-9]*)
Your second regex worked, because the + there was part of a character class and does not need to be escaped there.
01\+(?<cap>[\d.]*)
explain:
01 '01'
\+ '+'
[\d.]* any character of: digits (0-9), '.'
(0 or more times, matching the most amount possible)