Regex Explanation required - javascript

My Regex:
\d{0,2}\.?\d{0,3}r?em
string to match:
12123rem
Why is it matching 5 digits?
I want it to match the following pattern only:
12em or 12 rem or 12.345em or 12.345rem
up to 3 decimal places only.

Try this one:
^\d{0,2}(\.\d{1,3})?\s?r?em$
Here is a demo
^ - assures we are at the begining of the string
^\d{0,2} zero up to two decimals after begining of string
(\.\d{1,3}) optional group of 1 up to 3 digits with . in front
\s? optional whitespace character
r? optional r character
em mandatory em
$ end of the string

Related

Regular Expression to accept all the number with decimal from 0 to 168 followed by N or S

I have to build a regular exp which will accept all the numbers from 0 to 168 followed by "N" or "S".
I have prepared the following regular exp which is working fine except that it considers 168.11 as valid input.
Regular Exp:
/\b([1-9]|[1-9][0-9]|1[01][0-9]|16[0-8])(\.\d{1,2})?[N,S]?$/
Valid:
168S
11.2
10
140
125N
130S
3S
3.2
168.00S
Invalid:
168.11
168.12S
168.12N
Can anyone suggest what I am missing? Thank you!
You may use this regex with optional matches:
^\s*(?:(?:\d|[1-9]\d|1[0-5]\d|16[0-7])(?:\.\d{1,2})?|168(?:\.0{1,2})?)[NS]?$
RegEx Demo
RegEx Details:
^\s*: Match 0 or more horizontal spaces at start
(?:: Start non-capture group #1
(?:: Start non-capture group #2
\d: A single digit to numbers from 0 to 9
|: OR
[1-9]\d: Match numbers from 10 to 99
|: OR
1[0-5]\d: Match numbers from 100 to 159
|: OR
16[0-7]: Match numbers from 160 to 167
): End non-capture group #2
(?:\.\d{1,2})?: Match optional decimal point followed by 1 or 2 digits
|: OR
168: Match 168
(?:\.0{1,2})?: Match optional .0 or .00
): End non-capture group #1
[NS]?: Match optional N or S before end
$: End
You can also use
^(?:(?:[0-9]|\d\d|1[0-5]\d|16[0-7])(?:\.\d\d+)?|168(?:\.0+))[NS]?$
with regexr demo
What about:
^ *(?!0|168\.0?[1-9]|169)(?:\d?\d|1[0-6]\d)(?:\.\d?\d)?[SN]?$
See an online demo
^ - Start line anchor.
* - 0+ space characters.
(?!0|168\.0?[1-9]|169) - Negative lookahead for a leading zero or literally "168." followed by decimal number containing anything other than zero or literally "169".
(?: - Open non-capture group:
\d?\d - 1 Or 2 digits.
| - Or:
1[0-6]\d - A literal "1" followed by numbers 00 upto 69.
) - Close non-capture group.
(?:\.\d?\d)? - A 2nd non-capture group (optional) for a decimal number (1-2 digits).
[SN]? - Optionally match "S" or "N".
$ - End line anchor.

Making dot optional with limited characters

I am trying the following Regex and It is failing
/^\d{1,18}[.]?$/
I want digit 1-18 but a optional dot(.) anywhere. I tried the following too
/^[1-9]{1,18}[.]?$/
It counts . as a character as well i.e 12345678901234567.
How can I achieve 18 digits and an optional . anywhere in regex
You may use this regex with a lookahead to block 2 dots:
^(?!(?:\d*\.){2})[.\d]{1,18}$
RegEx Demo
RegEx Details:
^: Start
(?!(?:\d*\.){2}): Negative lookahead to disallow 2 dots
[.\d]{1,18}: Match dot or digit for length range between 1 to 18
$: End
You may use
^(?=(?:\.?\d){1,18}\.?$)\d*\.?\d*$
See the regex demo
Details
^ - start of string
(?=(?:\.?\d){1,18}\.?$) - a positive lookahead that requires 1 to 18 occurrences of an optional . and any digit followed with an optional . at the end of string
\d*\.?\d* - 0+ digits, an optional . and again 0+ digits
$ - end of string.

Testing if a string has only one charater with or without a space in the middle REGEX

I've this regex
`^([0-9]{2}|[0-9]{3}\s?[0-9]{3})\s?((?!(.)\3{7}))([0-9]{4}\s?[0-9]{4}$)`
These are the scenarios :
12 1234 1234 -> has to match
12 1111 1111 -> cannot match
12 12341234 -> has to match
12 11111111 -> cannot to match
I cannot find a way to solve this
If the 2 sets of 4 digits should not exist of the same digits, you could use a negative lookahead using an alternation which will match either the backreference to group 1 or a whitespace char.
^[0-9]{2}\s([0-9])(?!(?:\1|\s)+$)[0-9]{3}\s?[0-9]{4}$
Explanation
^ Start of string
[0-9]{2}\s Match 2 digits and whitespace char
([0-9]) Capture group 1, match the first digit of the first group of 4 digits
(?! Negative lookahead, assert what is on the right is not
(?:\1|\s)+$ Match the same digits using a backreference to group or a whitespace char
) Close lookahead
[0-9]{3}\s?[0-9]{4} Match 3 digits, optional whitespace char and 4 digits
$ End of string
Regex demo
Using the alternation at the start from your pattern to possibly also match 2 times 3 digits:
^(?:[0-9]{2}|[0-9]{3}\s?[0-9]{3})\s([0-9])(?!(?:\1|\s)+$)[0-9]{3}\s?[0-9]{4}$
Regex demo

Javascript Regular Expresion [duplicate]

I'm trying to write a RegExp to match only 8 digits, with one optional comma maybe hidden in-between the digits.
All of these should match:
12345678
12,45678
123456,8
Right now I have:
^[0-9,]{8}
but of course that erroneously matches 012,,,67
Example:
https://regex101.com/r/dX9aS9/1
I know optionals exist but don't understand how to keep the 8 digit length applying to the comma while also keeping the comma limited to 1.
Any tips would be appreciated, thanks!
To match 8 char string that can only contain digits and an optional comma in-between, you may use
^(?=.{8}$)\d+,?\d+$
See the regex demo
The lookahead will require the string to contain 8 chars. ,? will make matching a comma optional, and the + after \d will require at least 1 digit before and after an optional comma.
If you need to match a string that has 8 digits and an optional comma, you can use
^(?:(?=.{9}$)\d+,\d+|\d{8})$
See the regex demo
Actually, the string will have 9 characters in the string (if it has a comma), or just 8 - if there are only digits.
Explanation:
^ - start of string
(?:(?=.{9}$)\d+,\d+|\d{8}) - 2 alternatives:
(?=.{9}$)\d+,\d+ - 1+ digits followed with 1 comma followed with 1+ digits, and the whole string matched should be 9 char long (8 digits and 1 comma)
| - or
\d{8} - 8 digits
$ - end of string
See the Java code demo (note that with String#matches(), the ^ and $ anchors at the start and end of the pattern are redundant and can be omitted since the pattern is anchored by default when used with this method):
List<String> strs = Arrays.asList("0123,,678", "0123456", // bad
"01234,567", "01234567" // good
);
for (String str : strs)
System.out.println(str.matches("(?:(?=.{9}$)\\d+,\\d+|\\d{8})"));
NOTE FOR LEADING/TRAILING COMMAS:
You just need to replace + (match 1 or more occurrences) quantifiers to * (match 0 or more occurrences) in the first alternative branch to allow leading/trailing commas:
^(?:(?=.{9}$)\d*,\d*|\d{8})$
See this regex demo
You can use following regex if you want to let trailing comma:
^((\d,?){8})$
Demo
Otherwise use following one:
^((\d,?){8})(?<!,)$
Demo
(?<!,) is a negative-lookbehind.
/^(?!\d{0,6},\d{0,6},\d{0,6})(?=\d[\d,]{6}\d).{8}$/
I guess this cooperation of positive and negative look-ahead does just what's asked. If you remove the start and end delimiters and set the g flag then it will try to match the pattern along decimal strings longer than 8 characters as well.
Please try http://regexr.com/3d63m
Explanation: The negative look ahead (?!\d{0,6},\d{0,6},\d{0,6}) tries not to find any commas side by side if they have 6 or less decimal characters in between while the positive look ahead (?=\d[\d,]{6}\d) tries to find 6 decimal or comma characters in between two decimal characters. And the last .{8} selects 8 characters.

Regular expression for thousand separator without decimal point

I have regular expression which is working fine for decimal .
In below expression I want to remove decimal match to validate Integer
^-?(\d*)(,\d{1,3})*(?:[,]|([.]\d{0,2}))?$
Valid Match : 1,000 & 111, & -1000,00
Invalid Match : -,1 & 1,,,,
Use
^-?(?:\d{1,3}(?:,\d{3})*|\d+)?,?$
|---1---|
|---2-----|
|-3-|
The ^ asserts the position at the start of the string and $ asserts the position at the string end. -? matches an optional -. Part 1 matches 1 to 3 digits, Part 2 matches 0+ sequences of , followed with 3 digits, part 3 is an alternative to Part 1 & 2 combined and matches just 1+ digits. The whole number pattern is optional since the (?:...|...) is quantified with ? - one or zero occurrences.
The ,? before $ allows an optional comma at the end, delete if not required (you mentioned you need to allow 100, in the comments).
See the regex demo

Categories

Resources