I have the following regular expression to validate blood pressure values in the form of systolic/diastolic:
\b[0-9]{1,3}\/[0-9]{1,3}\b
The expression works with the only flaw that it allows more than one non-consecutive slash (/). For example, it allows this 2/2/2. I want it to allow only the format of a number from 1 to 999, and slash, and again a number from 1 to 999. For example, 83/23, 1/123, 999/999, 110/80, etc. Can anybody give me some help with this?
The only other expression I've found is here: ^\b(29[0-9]|2[0-9][0-9]|[01]?[0-9][0-9]?)\\/(29[0-9]|2[0-9][0-9]|[01]?[0-9][0-9]?)$, but it doesn't work.
Use ^ and $ to match the beginning and end of the string:
^\d{1,3}\/\d{1,3}$
By doing so, you force the matched strings to be exactly of that form.
Don't use the \b word-boundaries because a slash counts as a word boundary.
The use of ^ and/or $ is likely your most simple solution. Unfortunately, if your input is a part of a string or sentence or occurs more than once in a line, etc., you've got more thinking to do.
Expanding on Blender's answer, here is a simple check for validating BP value in the format: 120/80:
if(/^\d{1,3}\/\d{1,3}$/.test(120/80)) {
console.log("BP Valid");
} else {
console.log("Invalid BP");
}
^\b(29[0-9]|2[0-9][0-9]|[01]?[0-9][0-9]?)\/(29[0-9]|2[0-9][0-9]|[01]?[0-9][0-9]?)$
This is correct, the other had one extra \
Related
"abcd_" shouldn't be immediately followed by another underscore. Upon searching I found the regex [a-zA-Z0-9_] for allowing only alphanumeric chars and underscore.
I am finding difficulty to combine two or more conditions.To check the start string pattern was simple as-
static myValidator(control) {
if(control.value) {
if(control.value.match(/^abcd_/)) {
return null;
} else {
return {'invalidName':true};
}
}
}
^abcd_([a-zA-Z0-9][a-zA-Z0-9_]*)?$ if abcd_ is already valid by itself and nothing needs to follow.
Otherwise ^abcd_[a-zA-Z0-9][a-zA-Z0-9_]*$ requires at least one character after abcd_.
Or if there need to be at least 6 characters after abcd_: ^abcd_[a-zA-Z0-9][a-zA-Z0-9_]{5,}$
A regex typically reads left to right. In order to combine rules, just make sure you order them correctly. For instance checking /^abcd_/ will literally look for the substring abcd_ at the start of the string. To make sure the next symbol is alphanumeric but not an underscore, we might do /^abcd_[^_\W]/ which basically reads as "not an underscore and not, not an alphanumeric" since \W is equivalent to [^A-Za-z0-9_]. Lastly we check for zero or more alphanumeric characters with \w*$, note that this w is lowercase and is equivalent to [A-Za-z0-9_], and the * means 0 or more of the preceeding subexpression and the $ makes it non-greedy.
So we end up with a final regex of:
/^abcd_[^_\W]\w*$/i
Depending on exactly what you want to be able to match (hard to tell without any expected output) then it may need to be modified.
Check this link for example matches and a more in-depth explanation of what the regex does.
https://regex101.com/r/fzKhIx/3
I would also recommend reading this guide on regular expressions in javascript:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
Usually in my system i have the following string:
http://localhost/api/module
to find out the last part of the string (which is my route) ive been using the following:
/[^\/]+$/g
However there may be cases where my string looks abit different such as:
http://localhost/api/module/123
Using the above regex it would then return 123. When my String looks like this i know that the last part will always be a number. So my question is how do i make sure that i can always find the last string that does not contain a number?
This is what i came up with which really stricty matches only module for the following lines:
http://localhost/api/module
http://localhost/api/module/123
http://localhost/api/module/123a
http://localhost/api/module/a123
http://localhost/api/module/a123a
http://localhost/api/module/1a3
(?!\w*\d\w*)[^\/][a-zA-Z]+(?=\/\w*\d+\w*|$)
Explanation
I basically just extended your expression with negative lookahead and lookbehind which basically matches your expression given both of the following conditions is true:
(?!\w*\d\w*) May contain letters, but no digits
[a-zA-Z]+ Really, truly only consists of one or more letters (was needed)
(?=\/\d+|$)The match is either followed by a slash, followed by digits or the end of the line
See this in action in my sample at Regex101.
partYouWant = urlString.replace(/^.*\/([a-zA-Z]+)[\/0-9]*$/,'$1')
Here it is in action:
urlString="http://localhost/api/module/123"
urlString.replace(/^.*\/([a-zA-Z]+)[\/0-9]*$/,'$1')
-->"module"
urlString="http://localhost/api/module"
urlString.replace(/^.*\/([a-zA-Z]+)[\/0-9]*$/,'$1')
-->"module"
It just uses a capture expression to find the last non-numeric part.
It's going to do this too, not sure if this is what you want:
urlString="http://localhost/api/module/123/456"
urlString.replace(/^.*\/([a-zA-Z]+)[\/0-9]*$/,'$1')
-->"module"
/([0-9])\w+/g
That would select the numbers. You could use it remove that part from the url. What language are you using it for ?
Pardon my inexperience in Regex world.
I am trying to validate a expression which look like this : AB-4567 or PK-1234.
i.e. either set of 2 fix letters followed by a '-' and then digits with no constrain on length.
Few more valid examples:
AB-1234
AB-12
AB-54643564
PK-1
PK-341313
PK-133
So, it should start with either AB or PK then without any space hyphen and then any length of digits
I tried with /(AB)|(PK)[-][0-9]/ but it fails in following situation
ABPK-1213 (both set of prefix)
AB-R12U45N (alphabets in digits. either at start, middle or end)
I know I am missing something very basic but not able to solve it.
For the first six examples
/^(AB|PK)-[0-9]+$
If you want to include the other two possibility (ABPK-1213 , AB-R12U45N) try
/^(AB|PK|ABPK|PKAB)-[0-9A-Z]+$
You were almost there:
^(?:AB|PK)-[0-9]+$
Your alternation character | was in the wrong place. The way you had it, it meant "match AB OR match PK and all these other characters.
Can someone please explain the syntax of searching through strings? For example, I have this piece of code:
var ok = phone.value.search(/^\d{3}-\d{4}$/);
phone is a variable that is supposed to contain a phone number, and I know from context that this is supposed to make sure the inputted number has the format ###-####, but I don't know what the code within the parenthesis means or how it is evaluated. If someone has a link explaining how to use code like that I would especially appreciate it.
That's a regular expression ( regex ),
Regex One has a good guide on how to use them
Your regex says "beginning with 3 digits, then a "-" then 4 digits"
It's a regular expression, a whole world in itself.
http://www.regular-expressions.info/tutorial.html
It is regex object. The ^ matches the beggining of the string, the \d{3} matches 3 digits, the - matches a dash, the \d{4} matches for digits, and finally the $ matches the end of the string.
What you have there is called a "regular expression" and as you say, they are used to ensure input matches a certain pattern. I recommend you go somewhere like http://www.regular-expressions.info/ for further info rather than re-post data here.
I am trying to validate Phone Numbers that follow the next Pattern
01\d{9}
2\d{7}
But The user can enter more than one number separated by space or in one input field
So I come up with the regular expression
/(?:(?:01\d{9}$)|(?:2\d{7}$) ){1,}
A Test Sample
"01226113130 26322612 24586154 01004598654"
My Expression Doesn't match this sample, any help ?
Solution
For others if they fail in the problem, You can try Jerry Solution or this one
(?:(?:(?:01\d{9}(?:[\- \,])*)|(?:2\d{7}[\- \,]*))){1,}
Try this one:
^(?:(?:01\d{9}|2\d{7}) ){1,}(?:01\d{9}|2\d{7})$
Your current regex has (?:01\d{9}$)|(?:2\d{7}$) where the $ forced it to 'prematurely end' the match, so removing this was the first thing to do. Then (?:01\d{9})|(?:2\d{7}) can be re-written as (?:01\d{9}|2\d{7}). I added a ^ for the beginning of the string.
Afterwards, this regex will only validate strings ending with a space, so add another (?:01\d{9}|2\d{7}) at the end and finally conclude with $.
regex101 demo.
Oh, also, it might be better to turn the {1,} into * like this:
^(?:(?:01\d{9}|2\d{7}) )*(?:01\d{9}|2\d{7})$