I need two regular expression.
1) Any characters followed by a comma followed by any characters followed by a comma followed by any characters.
Currently I have:
/(.*,.*,.*)/
2) Any characters followed by a comma followed by any characters so long as they are not a comma.
Currently I have:
/(.*,.*[^,]+.*)/
Any help would be much appreciated. Thanks in advance!
For your first Regex you could really just use Javascript built in string.split(","); Which would return an array of strings. From there run a check for array.length >= 3 and you'll know the string matched you pattern. That being said f there are commas in the characters after that second required comma you could have issues depending on what you are expecting.
The second Regex could be verified using string.split(",") as well. Your second check would just be array.length === 2
The full code would be something like this
function verify(str) {
var arr = str.split(",");
if (arr.length < 2)
return "Invalid string provided";
if (arr.length === 2)
return arr[0] + arr[1];
return join(arr);
}
verify("some,crazy,comma delimited voodoo");
For your first regular expression, /(.*,.*,.*)/, you have what you're looking for. Note that "any character" includes commas, so really this only guarantees that there will be at least 2 commas.
For the second, /(.*,.*[^,]+.*)/, that's not quite right. "Any characters followed by a comma followed by any characters so long as they are not a comma" would be /(.*,[^,]*)/. However again, consider that "any characters" includes commas, so it is somewhat meaningless.
Perhaps you mean something more like /([^,]*,[^,]*)/? This is any text with precisely one comma. You can repeat the pattern by adding a second ,[^,]* as you wish, for example /([^,]*,[^,]*,[^,]*)/ is for precisely two commas.
If you wish to match any number of items separated by commas, try /([^,]+,?)/g to match each individual list item.
If you also require that there is text between commas, use + instead of *.
I don't know the utility of your regular expressions but in the linux grep command this will work:
grep '?,?,?'
grep '?,*[^,]'
Related
I was trying to match the ordered letters using regular expression in php/javascript.
I have a 4 letter word in which first 2 letters should be in order and the next two letters should be in order like BCEF. This I wanted to match using regular expression.
But the below regular expression is also matching the order CBFE
Please suggest what's missing in the below expression to match the letters order. Thank you.
[A-H]{2}[D-M]{2}
I would not use a regex but php code instead :
$s = "BCEF";
$arr = str_split($s);
if ($arr[0] <= $arr[1] && $arr[2] <= $arr[3]) {
// Your string matched
}
Here's a solution using regex (but mainly to illustrate how stupid it is ;).
(?:A[B-H]|B[C-H]|B[C-H]|D[E-H]|E[FGH]|F[GH]|GH)(?:D[E-M]|E[F-M]|F[G-M]|G[H-M]|H[I-M]|I[J-M]|J[KLM]|LM)
It has two (non capturing) groups following one another - one for each letter pair.
They tests for all possible start characters (A to G for the first pair and D to L for the second) being followed by any of the allowed subsequent characters, using alternation.
See it here at regex101.
I need a regular expression allow comma after a number. i have tried But after number it allowing commas 122,,,
I want:
12,323,232,2,232
1,1,1,1,1
123123,23231,2322
I don;t want:
12312,,,123,,2,32,
12312,123,12,,,,,123,12
My code is
$(".experience").keyup(function (e) {
this.value = this.value.replace(/[^0-9\{0,9}]/g,'');
});
Is this what you're looking for? I'm not used to working with regex's, but managed to quickly put this together:
^([0-9]+\,?)*$
The questionmark is makes the comma optional.
[0-9] = numeric values only
+ = quantifier between 1 and unlimited
\ = escapes the character, so it's a constant
* = quantifier between 0 and unlimited times
Hope this helps!
Regards
If I understood your question correctly, you want to remove any commas not succeeding a number, and also remove any characters other than digits or commas. This is fairly simple if your language supported lookbehinds, however it isn't that hard in javascript too. You could use the below regex to match any incorrect commas and then replace them with $1
/^,+|(,)+|[^0-9,]+/g
Replace With: $1
Any commas at the start should be replaced with an empty string ''
Any commas which are consecutive i.e ,+, they should be replaced by a single comma i.e ,
Any characters other than digits or comma should be replaced with an empty string ''
To combine these two rules, ^,+|(,)+ will help match both and the replace $1 corresponds to capturing group 1, which will only be present in the 2nd condition so it will replace multiple commas with a single comma i.e (,)+ is replaced with (,). Whereas in the first alternative ^,+ which matches commas at the starting, there is the first capturing group remains empty so it gets replace with empty string ''
Here's a js demo:
$(function() {
$(".experience").keyup(function (e) {
this.value = this.value.replace(/^,+|(,)+|[^0-9,]+/g,'$1');
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class='experience' name="experience" value=""/>
You could consider changing keyup to focusout instead, although it's upto you! :)
Regex101 Demo
I am not very good at regex and I need 3 different regex's that follow the following rules:
I need a regex that only allows one comma in string
I need a regex that doesn't allow a comma at the start of a string but still allow only one comma.
I need a regex that doesn't allow a comma at the end of a string but still allow only one comma.
For the first rule: 21, day would be okay but 21, day, 32 would not be okay.
For the second rule: ,21 would not be okay.
For the third rule: 21, would not be okay.
So far I have created a regex below which accommodates for the rules above but I was wondering if it could be split up into three different regex's that can accommodate for the three above rules.
^[a-z0-9A-Z&.\/'_ ]+(,[a-zA-Z0-9&.\/'_ ]+)?$
Any help would be much appreciated.
Thanks
Allow only one comma (put a ? behind the second comma if you want to make the comma optional):
^[^,]*,[^,]*$
Allow only one comma but none at the beginning:
^[^,]+,[^,]*$
Allow only one comma but none at the end:
^[^,]*,[^,]+$
[^,] means "a character that is not a comma".
This regex should do the job.
^[^,]+,[^,]+$
Explanation:
[^,]+ -> Not comma at least once
, -> comma (obviously)
[^,]+ -> Not comma at least once (again)
Simple string operations can handle this:
function testForComma(str) {
//Comma isn't first character
//Comma only exists once
//Comma isn't last character
return str.indexOf(',') >= 1 &&
str.lastIndexOf(',') == str.indexOf(',') &&
str.lastIndexOf(',') != str.length - 1;
}
console.log(testForComma(','));
console.log(testForComma(', '));
console.log(testForComma(' ,'));
console.log(testForComma(' , '));
console.log(testForComma(' ,, '));
simple expression is like - ^[^,]+,[^,]+$
^(?!.*,.*,) - this is the base regex rejecting recurrent commas.
^(?!,)(?!.*,.*,) - the same base with added "no comma at the start" condition
^(?!.*,.*,).*(?:[^,]|^)$ - the same base with "no comma at the end". The latter is implemented as alternation group match since lookbehinds are not available in JavaScript.
Note: all these patterns allow zero or one comma in the input. If you need strictly one comma, prepend each of them with ^(?=.*,).
I am using following regex if I pass lenghty string to check, regex101.com showing timeout message. Is there any ideal length to test regular expresssion?
^(\d+[\s\r\n\t,]*){1,100}$
https://regex101.com/r/eC5qO7/1
I suggest running a split, then making sure what is split is a number, so:
var test = "123,456 789 101112 asdf";
var numbers = test.split(/\s*,\s*|\s+/);
numbers.forEach(function(n) {
if (n % 1 !== 0) {
alert(n + " is not an integer");
}
});
The catastrophic backtracking is caused by the fact that the [\s\r\n\t,] character class has a * quantifier applied to it, and then a + quantifier is set to the whole group. The regex backtracks into each digit to search for optional whitespace and commas, which creates a huge number of possibilities that the engine tries before running into the "catastrophe".
Besides, there is another potential bottleneck: \s in the character class can also match \r, \n and \t. See see Do character classes present the same risk?.
Without atomic groups and possessive quantifiers, the regex optimization is only possible by means of making one of the "separators" obligatory. In this case, it is clearly a comma (judging by the example string). Since you just want to validate the number of input numbers separated with commas and optional spaces, you can use a simpler regex:
^(?:[0-9]+\s*,\s*){1,100}$
Here, it fails gracefully, and here it matches the string OK.
If a comma at the end is optional, use
^(?:\d+\s*,\s*){1,99}\d+,?\s*$
See demo
Also note you do not need the i modifier, as there are no letters in the pattern.
I'm trying to build up a regex pattern for the html input field which only allows up to 20 combined alphabetical letters and digits which can only have up to two of Dashes(-), Underscores(_) and fullstops (.)
So something like only two of the symbols allowed and any amount of letters and digits allowed, combined they've got to be between 4 and 20.
What would the pattern for this be?
An sample (non functioning) version could be like [A-Za-z0-9([\._-]{0,2})]{4,20}
Solution:
I decided to go with #pascalhein #Honore Doktorr answer which is to use a lookahead.
The final pattern is ^(?=[A-Za-z0-9]*([._-][A-Za-z0-9]*){0,2}$)[A-Za-z0-9._-]{4,20}$
You can verify the length with a lookahead at the beginning:
^(?=.{4,20}$)
Then list all the cases that are allowed for your regex separately:
[A-Za-z0-9]* (no special chars)
[A-Za-z0-9]*[._-][A-Za-z0-9]* (one special char)
[A-Za-z0-9]*[._-][A-Za-z0-9]*[._-][A-Za-z0-9]* (two special chars)
It isn't beautiful, but I believe it should work. This is the final expression:
^(?=.{4,20}$)([A-Za-z0-9]*|[A-Za-z0-9]*[._-][A-Za-z0-9]*|[A-Za-z0-9]*[._-][A-Za-z0-9]*[._-][A-Za-z0-9]*)$
Edit:
Actually, it might be nicer to test the number of special characters with a lookahead instead:
^(?=[A-Za-z0-9]*([._-][A-Za-z0-9]*){0,2}$)[A-Za-z0-9._-]{4,20}$
I encourage you to not to use a Regex when there are functions for the porpuse you want to achieve.
An advice I give to jrs on this topic is to use a regex for single porpuses, if there is more than one porpuse, use more regex.
to answer your question:
1 your valid characters from start to end.
var 1stregex = /^[A-Za-z0-9._-]{4,20}$/;
2 Count must be 0 to 2, which in javascript is .match().length.
var 2ndregex = /([._-])/;
if(myText.match(1stregex) && myText.match(2ndregex).length <=2)
{ isvalid=true; }