I need to create a regular expression for a string in the format
<whole-number>#<whole-numbers>%
for example:
1#100%,
9#50%,
5#10%,
only single numeric digit should be allowed before #
only # special character after the numeric digit
there should be only 3 or less numeric digits after the #
only % special character should be allowed at the end of the string.
the following examples are not valid.
0#100%,
a#50%,
1#abc%,
I have created a regular expression but it is not working as expected
([0-9]{1}[#]{1}[0-9]{0,3})
This should do it:
/^\d#\d+%$/
\d is for digits. + says one or more characters
The way you have written your regex, it seems you only need 3 numbers after #, if you want that then the way in which you have written it, it should be something like:
/^[0-9]{1}#[0-9]{0,3}%$/
You were missing the % character match and the anchor tags.
Or else you could just use:
/^\d#\d{0,3}%$/
You want the string to start with a one digit number from 1 to 9 (not 0), followed by #, then by a number with at most 3 digits and ending with %.
Then use:
/^[1-9]#\d{1,3}%$/
console.log('4#55%',/^[1-9]#\d{1,3}%$/.test('4#55%'))
console.log('2#678%',/^[1-9]#\d{1,3}%$/.test('2#678%'))
console.log('22#22%',/^[1-9]#\d{1,3}%$/.test('22#22%'))
console.log('a#11%',/^[1-9]#\d{1,3}%$/.test('a#11%'))
console.log('0#99%',/^[1-9]#\d{1,3}%$/.test('0#99%'))
console.log('3#%',/^[1-9]#\d{1,3}%$/.test('3#%'))
console.log('1#abc%',/^[1-9]#\d{1,3}%$/.test('1#abc%'))
Related
I am trying to parse a string which has two numbers, both can be between 1 and 3 digits, and will have a colon in between. Here are some examples:
"1:1"
"1:12"
"12:1"
"123:12"
Also, the given string may also be invalid, and I need to detect if it is. My attempts so far to make sure the string is valid have looked like this: .match(/[1-9]\:[1-9]/);. But then I noticed that this wont work if a string such as this is inputted: "characters12:4characters". How would I go about validating the string to make sure it is in the format x:y?
Any help would be deeply appreciated.
Edit: numbers which contain 0 at the beginning is valid, but may not be given.
You may use
/^\d{1,3}:\d{1,3}$/
See the regex demo
Details
^ - start of a string
\d{1,3} - one, two or three digits (\d is a shorthand character class that matches any digit (it can also be written as a [0-9] character class) and {1,3} is a limited quantifier that matches1 to 3 consecutive occurrences of the quantified subpattern)
: - a colon
\d{1,3} - one, two or three digits
$ - end of the string.
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.
I am working on a JavaScript regular expression for the following condition check.
Consignment number validation details:
Cnote length :12 Varchar
First Character should be Character Upper Case
Fifth Character may be character or integer
Remaining all integer
Examples of valid strings:
C991S1234567
C30811234567
I have no idea. I have tried a simple regular expression like checking only numbers or alphabets.
I have tried something like this:
^[0-9]
It allows only integers. I do not know how to add constraints to it. Any help will be greatly appreciated.
The regex for this task relies on character classes ([...]) and limiting quantifiers ({n}). ^[0-9] only checks if the first character is a digit.
You can use the following regex:
^[A-Z][0-9]{3}[a-zA-Z0-9][0-9]{7}$
See demo
Explanation:
^ - Beginning of the string
[A-Z] - First is an uppercase English letter
[0-9]{3} - The 2nd, 3rd and 4th characters are digits
[a-zA-Z0-9] - Fifth character is either a letter or digit
[0-9]{7} - Following 7 characters are digits
$ - End of string.
I am trying to write a regular expression for a bit of javascript code that takes a user's input of a mobile number and in one regular expression, performs the following checks:
Starts with 07
Contains only numbers, whitespace or dashes
Contains exactly 11 numbers
Is this possible to do in just one regular expression and if so, how please?
I don't think it is possible with one regex, but it is possible by testing for two conditions:
if(/^07[\d\- ]+$/.test(str) && str.replace(/[^\d]/g, "").length === 11) {
//string matches conditions
}
Explanation of the regex:
^: Anchor that means "match start of string".
07: Match the string 07. Together with the above, it means that the string must start with 07.
[: Beginning of a character class i.e., a set of characters that we want to allow
\d: Match a digit (equivalent to 0-9).
\-:
" ": Match whitespace (markdown doesn't let me show a single space as code)
]: End of character class.
+: One or more of the previous.
$: Anchor that means "match end of string". Together with the ^, this basically means that this regex must apply to the entire string.
So here we check to see that the string matches the general format (starts with 07 and contains only digits, dashes or spaces) and we also make sure that we have 11 numbers in total inside the string. We do this by getting of anything that is not a digit and then checking to see that the length of the string is equal to 11.
Since #Vivin throws out the challenge :
/^07([-\s]*\d){9}[-\s]*$/
^07 : begin with digits 07
( : start group
[-\s]* : any number of - or whitespace
\d : exactly one digit
){9} : exactly 9 copies of this group (11 digits including 07)
[-\s]* : optional trailing spaces or -
$ : end of string
Of course a more useful way might be as follows
if ((telNo = telNo.replace (/[-\s]+/g, '')).match (/^07\d{9}$/)) {
....
}
which has the advantage (?) of leaving just the digits in telNo
Thank you all for trying, but after a good while trying different ideas, I finally found a working "single" regular expression:
07((?:\s|-)*\d(?:\s|-)*){9}
This make sure that it starts with 07, only contains digits, whitespace or dashes, and only 11 of them (9 plus the first 2) are numbers.
Sorry to have wasted your time.
Explanation:
() - include in capture
(?:) - do not include in capture
\s - whitespace
| - or
- - dash
* - zero or more
\d - digits only
{9} - exactly nine of what is captured
I have following regular expression: ^-?([0-9]{0,3}$|[0-9]{0,2}\.?[0-9]{0,1}$)
It should not allow 4 digit number such as 4444. The expression is working fine if I try here, but in javascript, the code is not working as expected. It is allowing 4 digit numbers. All other validations work fine.
Here is my code:
http://jsfiddle.net/PAscG/
reg0str = "^-?([0-9]{0,3}$|[0-9]{0,2}\.?[0-9]{0,1}$)";
var reg0 = new RegExp(reg0Str);
if (reg0.test(temp)) return true;
UPDATE TO EXPLAIN Functionality:
I want to allow only 3 digits. So either I can allow only 1 digit after decimal and 2 before decimal or I can allow max of 3 digits before decimal and nothing after decimal.
So my first part:
[0-9]{0,3}$ I assume this should allow a max of 3 digits and only numbers.
Next part: [0-9]{0,2}\.?[0-9]{0,1}$ should allow max of 2 digits before decimal and a max of 1 digit after decimal.
Following OP's clarification
The regexp is
/^-?(\d{0,3}\.?|\d{0,2)\.\d)$/
^ start of string
-? optional minus sign (use [-+]? if you accept a plus sign)
( start of OR group
\d{0,3} 0 1, 2 or 3 digits
\.? optional decimal point
| OR
\d{0,2} 0 1, or 2 digits
\. decimal point
\d final decimal
) end of OR grouping
$ end of string
Try this:
var reg0str = "^\-?[0-9]{0,2}[\.]?[0-9]?$";
I'm not sure why, but the period seems to be being treated as the wildcard character if not encapsulated within a class.
Here's the updated jsfiddle
"…\.…" is a string literal - the backslash escapes the dot to a dot and the regex dot matches a digit. You would need to escape the backslash to pass a string with a backslash in the RegExp constructor:
new RegExp("^-?([0-9]{0,3}$|[0-9]{0,2}\\.?[0-9]{0,1}$)")
or you use a regex literal (simplified, but still matching the same):
/^-?\d{0,2}\.?\d?$/