I am new at using the regex.
I want to allow strings with following format:
(Any number of digits).(two digits after .)
e.g. 11.34, 111.78, 132323.78
Can anybody please help me in this?
That would be \d+\.\d\d
\d means any digit
+ means one or more
\. means a literal dot.
EDIT
if you actually mean "two or more digits after" as Mu Mind suggests try the following:
\d+\.\d\d+
\d+\.\d\d
That will match 1 or more digits, then a dot, then 2 digits. (those last two digits must exist in this case.
Fixed: Extra dot
Related
I want the EURO format to be accepted only in the following forms.
Three digits with "." Be segregated.
i found this expression but not work in my way:
^(0|(([1-9]{1}|[1-9]{1}[0-9]{1}|[1-9]{1}[0-9]{2}){1}(\ [0-9]{3}){0,})),(([0-9]{2})|\-\-)([\ ]{1})(€|EUR|EURO){1}$
I want it to be exactly like the following:
1.123,45
1,23
1.234.567,45
0,56
I want two digit cent to be mandatory.
thanks
You can try
^(?!0\d)\d{1,3}(?:\.\d{3})*,\d{2}$
^ start of the string
(?!0\d) negative lookahead, make sure there's no leading 0s like 01,25, remove it if it is allowed
\d{1,3} 1 to 3 digits
(?:\.\d{3})* followed by any occurances of texts like .123
,\d{2} followed by 2 decimal digits, mandatory. If you want it to be optional, replace it with (?:,\d{2})?
$ end of the string
See the test cases
-?(\d{1,3})(\.\d{3})*(,\d{2})? (€|EURO?)
This will capture the numbers. You never ever need {1} because that is the default assumption. Also spaces don't need to be escaped.
I need to figure out how to make my regex allow match correctly each time I type a number/decimal point. I want to limit the number of digits before and after the decimal point, which isnt too hard but i cant figure out how to allow the decimal point to match as well.
1 - match
12 - match
1234 - match
12345 - wrong
1234. - match
1234.1 - match
1234.12 - match
1234.123 - wrong
Other matched numbers
12.12
1.0
123.99
Edit:
So I want a max of 4 numbers before the decimal place and two after. Also the decimal place is optional.
The tricky part is that I want it to fail if the fifth character isn't a decimal point.
You need to specify your constraints better; I'm assuming you want a maximum of 4 before the dot and 2 after:
/^\d{1,4}(\.\d{0,2})?$/
edit: I added beginning and end of string matchers. Should work as you want now
You can use the following regex to select only those words that consists of digits and satisfying your condition.
/(?<=^|\s)\d{1,4}(?:\.\d{0,2})?(?=\s|$)/g
Positive lookahead and lookbehind are used to make sure that a whitespace is around the number.
DEMO
Debuggex Demo
Something like this will help
r'^\d{1,4}(\.\d{0,2})?$'
As you must be aware, \d represents a digit, . for the decimal point and {min_required,max_required}. Be sure to test your regular expression prior to using them here.
I'd appreciate if someone could help..
I need to allow the occurrence of dash (-) in any position of the string (except the beginning).
My RegEx is:
^[+]?[0-9]{3,10}$
I want to allow the following possibilities:
+7-777-77777
7-7-7-7-7-77
etc. so that I could have dash in any place after plus (+) and first digit.
Thank you in advance!
You can use lookahead
^(?=([^\d]*\d){3,10}[^\d]*$)[+]?\d+(-\d+)*$
--------------------------
|
|->match further only if there are 3 to 10 digits in string
This would match string with three to 10 digits optionally having - in between the string
try it here
If you want optional space in between the strings
^(?=([^\d]*\d){3,10}[^\d]*$)[+]?\d+(\s*-\s*\d+)*$
http://rubular.com/r/8eyAolNHlX
In Ruby it should Works:
/\+?(?:\d\-?\d?)+/
Maybe this?
^[+]?\d(-?\d){2,9}$
I think the pattern you're looking for is this:
^[+]?[0-9][0-9-]{2,9}$
This will match an optional plus, followed by a decimal digit, followed 2 to 9 decimal digits or hyphens.
If you also want to ensure that the string doesn't end with a hyphen, simply use this:
^[+]?[0-9][0-9-]{1,8}[0-9]$
This will match an optional plus, followed by a decimal digit, followed 1 to 8 decimal digits or hyphens, followed by a decimal digit.
Note you can also extend this to all Unicode digits (see this answer for more information):
^\+?\d[\d-]{2,9}$
or
^\+?\d[\d-]{1,8}\d$
This requires the input start and end with a digit with optional plus at front
^\+?\d[\d-]{,8}\d$
I want users to be allowed to enter numbers, up to 3 digits before the decimal place, with an optional decimal place and a maximum of 2 digits after the optional decimal place.
I want it to match: 12, 123, 123.5, 123.55, 123.
I do not want it to match: abc, 1234, 123.555
What I have so far it:
^\d{0,3}(.?)\d{0,2}$
At the moment it is still matching 1234. I think I need to use the look behind operator somehow but I'm not sure how.
Thanks
Try this:
^\d{0,3}(?:\.\d{0,2})?$
Or better, to avoid just a .:
^(?:\d{1,3}(?:\.\d{0,2})?|\.\d{1,2})$
Specifically, note:
Escaping the dot, or it matches any character (except new lines), including more digits.
Made the whole decimal part optional, including the dot. That is - the decimal dot is not optional - it must be including if we are to match any digit from the decimal part.
Even if you have escaped the dot, ^\d{0,3}(\.?)\d{0,2}$ isn't correct. With the dot optional, it can match 12378: \d{0,3} matches 123, (\.?) doesn't match anything, and \d{0,2} matches 78.
Working example: http://rubular.com/r/OOw6Ucgdgq
What about this?
/^\d{0,2}(?:\d\.|\.\d|\d\.\d)?\d?$/
Maybe this (untested)
^(?=.*\d)\d{0,3}\.?(?<=\.)\d{0,2}$
Edit - the above is wrong.
#Kobi's answer is correct.
A lookahead could be added to his first version to insure a NOT just a dot or empty string.
^(?=.*\d)\d{0,3}(?:\.\d{0,2})?$
You have to put the combination of decimal point and the decimal numbers optional. In your regex, only the decimal number is optional. 1234 is accepted because 123 satisfy ^\d{0,3}, not existing decimal point satisfy (.?), and 4 satisfy \d{0,2}.
Kobi's answer provided you the corrected regex.
I need a regular expression for validation two or one numbers then , or . and again two or one numbers.
So, these are valid inputs:
11,11
11.11
1.1
1,1
\d{1,2}[\,\.]{1}\d{1,2}
EDIT: update to meet the new requirements (comments) ;)
EDIT: remove unnecesary qtfier as per Bryan
^[0-9]{1,2}([,.][0-9]{1,2})?$
In order to represent a single digit in the form of a regular expression you can use either:
[0-9] or \d
In order to specify how many times the number appears you would add
[0-9]*: the star means there are zero or more digits
[0-9]{2}: {N} means N digits
[0-9]{0,2}: {N,M} N digits to M digits
Lets say I want to represent a number between 1 and 99 I would express it as such:
[0-9]{1,2} or \d{1,2}
Or lets say we were working with binary display, displaying a byte size, we would want our digits to be between 0 and 1 and length of a byte size, 8, so we would represent it as follows:
[0-1]{8} representation of a binary byte
Then if you want to add a , or a . symbol you would use:
\, or \. or you can use [.] or [,]
You can also state a selection between possible values as such
[.,] means either a dot or a comma symbol
And you just need to concatenate the pieces together, so in the case where you want to represent a 1 or 2 digit number followed by either a comma or a period and followed by two more digits you would express it as follows:
[0-9]{1,2}[.,]\d{1,2}
Also note that regular expression strings inside C++ strings must be double-back-slashed so every \ becomes \\
\d means a digit in most languages. You can also use [0-9] in all languages. For the "period or comma" use [\.,]. Depending on your language you may need more backslashes based on how you quote the expression. Ultimately, the regular expression engine needs to see a single backslash.
* means "zero-or-more", so \d* and [0-9]* mean "zero or more numbers". ? means "zero-or-one". Neither of those qualifiers means exactly one. Most languages also let you use {m,n} to mean "between m and n" (ie: {1,2} means "between 1 and 2")
Since the dot or comma and additional numbers are optional, you can put them in a group and use the ? quantifier to mean "zero-or-one" of that group.
Putting that all together you can use:
\d{1,2}([\.,][\d{1,2}])?
Meaning, one or two digits \d{1,2}, followed by zero-or-one of a group (...)? consisting of a dot or comma followed by one or two digits [\.,]\d{1,2}
\d{1,2}[,.]\d{1,2}
\d means a digit, the {1,2} part means 1 or 2 of the previous character (\d in this case) and the [,.] part means either a comma or dot.
Shortest regexp I know (16 char)
^\d\d?[,.]\d\d?$
The ^ and $ means begin and end of input string (without this part 23.45 of string like 123.45 will be matched). The \d means digit, the \d? means optional digit, the [,.] means dot or comma. Working example (when you click on left menu> tools> code generator you can gen code for one of 9 popular languages like c#, js, php, java, ...) here.
[ // tests
'11,11', // valid
'11.11',
'1.1',
'1,1',
'111,1', // nonvalid
'11.111',
'11-11',
',11',
'11.',
'a.11',
'11,a',
].forEach(n=> console.log(`${n}\t valid: ${ /^\d\d?[,.]\d\d?$/.test(n) }`))
If you want to be very permissive, required only two final digits with comma or dot:
^([,.\d]+)([,.]\d{2})$