I have to write a regex that contains either the words “wind”, “temp”, or “press” followed by a non-digit.
So far I have:
var regex = /wind|temp|press[^0-9]{0,}/;
This doesn’t work because the [^0-9]{0,} is with “press”. How would I separate them so that all the words would be read followed by a non digit?
Just use a (non-capturing) group:
var regex = /(?:wind|temp|press)[^0-9]{0,}/;
All you need is a none capturing group to separates those words from the rest of your pattern. Use this pattern:
/(?:wind|temp|press)\D*/
By the way {0,} is the same as * in this case. Also if being a non-digit character is mandatory, you probable want to use + instead. (I mean if one of those words must be followed by at least one or more non-digit character, then use +)
Online Demo
Related
I have the following string:
"By signing in, I agree to the {{#a}}[Terms of Use](https://www.example.com/termsofuse){{/a}} and {{#a}}[Privacy Policy](https://www.example.com/privacy){{/a}}."
And I am using the following regex to split the words while considering {{#a}}[Terms of Use](https://www.example.com/termsofuse){{/a}} and {{#a}}[Privacy Policy](https://www.example.com/privacy){{/a}} as whole words.
\s+(?![^\[]*\])
My problem is that my current regex does not remove the full stop at the end of {{#a}}[Privacy Policy](https://www.example.com/privacy){{/a}}.. Ideally I would like my regex to split full stops, exclamation marks and question marks. That being said, I'm not sure how would I differentiate between a full stop at the end of the word and a full stop that is part of the URL.
You can try a variation of the following regular expression:
\s+(?![^\[]*\])|(?=[\.?!](?![a-zA-Z0-9_%-]))
The new part being the alternation of (?=[\.?!](?![a-zA-Z0-9_%-])) at the end. It performs a positive lookahead of a period, question mark or bang, using a negative lookahead to make sure it's not followed by a URL-ish looking character. You may need to adjust that character class in brackets to contain the characters you want to consider part of the URL.
Instead of .split you will be better off using .match here using this regex:
\{\{#a}}.*?\{\{\/a}}/g
This matches {{#a}} followed by 0 or of any character followed by {{/a}}.
or else you may use this more strict regex match:
\{\{#a}}\[[^\]]*]\([^)]*\)\{\{\/a}}
Here:
\[[^\]]*]: Matches [...] substring
\([^)]*\): Matches (...) substring
RegEx Demo
var string = "By signing in, I agree to the {{#a}}[Terms of Use](https://www.example.com/termsofuse){{/a}} and {{#a}}[Privacy Policy](https://www.example.com/privacy){{/a}}.";
console.log( string.match(/\{\{#a}}.*?\{\{\/a}}/g) );
Basically i have String restrict = "Hello+Hi"; which i want to restrict all characters other than (/^[a-zA-Z0-9~!##\(\.)]$/) using regex.
in javascript this is how it's being done
field.value.match(/^[a-zA-Z0-9~!##$%&^*()-_=.<>?)\(\)]+$/)
which will restrict all the characters between parenthesis.
i want my string to only contain (/^[a-zA-Z0-9~!##\(\.)]$/)
i really appreciate if somebody tell me how to do this in java.
You can use String.matches, which will match the whole String with a pattern.
In this case: "Hello+Hi".matches("\\p{Alpha}+") will return false because + is not a word character.
To avoid confusion: the + in the pattern is a greedy quantifier for 1+ character repeats.
The p{Alpha} represents alphabetic characters, and requires double-escaping.
See docs here.
Edit
Since you edited your requirement, just use the custom class as follows, plus the quantifier:
"Hello+World".matches("[a-zA-Z0-9~!##\\().]+") // returns false because of the +
I am trying to find all occurrences of a special character / surrounded by either letters or numbers.
After many tries, I have come up with the following Regex that almost does what I need:
(?![a-z0-9])\/(?=[a-z0-9])
This works fine for these examples:
aa/aa
123/123
aa/123
However, it fails if there are two forward slashes together:
http://regexr.com/
In this case, it matches the second forward slash after http which I do not want.
How can I modify this Regex to meet my needs?
EDIT: I do not want to a match when two forward slashes are together. I only want to match if a single forward slash is surrounded by alphanumeric characters.
you would need a positive lookbehind group, like so:
(?<=[a-z0-9])+\/{1}(?=[a-z0-9]+)
however, according to http://regexr.com/ it is not supported in javascript.
Works fine in e.g. python http://pythex.org/
Easy!
(?![a-z0-9])\/+(?=[a-z0-9])
You should have put + for 1 on more occurrence of a character. So you should have written \/+ instead of just \/.
Try this
(!?[a-z0-9])\/(?=[a-z0-9])
Try this
[a-z0-9](\/)[a-z0-9]
Regex demo
Explanation:
( … ): Capturing group sample
\: Escapes a special character sample
NKA-198, HM-1-0022, SCIDG 133
want regexp for the above codes. How can I Accept these codes and assign it to a variable??
Please suggest me and Thanks in advance.
First make sure that you have a solid understanding of the general structure of the strings you want to match - e.g., which separator symbols will be permissible (your example suggests -, SPC, but what about +? Would you want to match NKA 198, SCIDG-133 too ?
As a base for further refinement, use the following code fragment:
var orig = "some string containing ids like 'NKA-198' and 'SCIDG 133'";
var first_id = orig.replace(/^.*?([A-Z]+([ -][0-9]+)+).*/, "$1");
var last_id = orig.replace(/(?:.*[^A-Z]|^)([A-Z]+([ -][0-9]+)+).*/, "$1");
Explanation
core( ([A-Z]+([ -][0-9]+)+) )
Match any sequence of capital letters followed by a digit sequence preceded by a single hyphen or space character. The sequence 'space or hyphen plus number' may repeat arbitrarily often but at least once. This specification may be too restrictive or too lax which is the reason why you have to look up / guess general rules that the Ids you wish to match obey. In a strict sense, the regex you've been asking for is ^(NKA-198|HM-1-0022| SCIDG 133)$, which most certainly is not what you need.
The outermost parentheses define the match as the first capture group, allowing to reference the matched content as $1 in the replace method. Using replace also mandates that your regexp needs to match the whole original string.
additional parts / first regexp
Matches anything non-greedily, starting at the string's beginning. The non-greedy operator (.*?) makes sure that the shortest possible match is found that still allows a match of the complete pattern (See what happens if you drop the question mark). Ths you'll end up with the first matching id in first_id.
additional parts / second regexp
Matches greedily (= as much as possible) until an identifier pattern matches. Thus you'll end up with the last match. the negated character class ([^A-Z]) is necessary, since you there is no further information about the structure of the IDs in question, specifically which/how many leading capital characters there are. The class makes sure that the last character beforethe beginning of the matched id is not a capital character. The ^ in the alternation caters for the special case that orig starts with a matchable ID - in this case, the negated char class would not match, because there is no 'last prefix character' before the match.
References
A more detailed (and more competent) explanation of regexp pattern and usage can be found here. MDN provides info on regular expression usage in javascript.
I'm trying to write a regular expression in JS to recognize any digit up to seven times, followed by a "-" followed by 2 digits followed by "-" followed by a single digit. This is the simple regex I have:
/\d{1,7}-\d{2}-\d/g
This should match strings like:
123-12-7
1-12-7
1234567-12-7
but not 12345678-12-1
However, the above is returning true. The regex returns true when there is any number of digit in the first group.
Does the JavaScript Regex object not support {n,m}?
Here is an example of what I am talking about.
var pattern = new RegExp(/\d{1,7}-\d{2}-\d/);
alert(pattern.test("12345678-13-1"));
http://jsfiddle.net/XTRAc/1/ live example
It matches 2345678-13-1. You need to anchor it to the beginning and end of your string:
/^\d{1,7}-\d{2}-\d$/
Note though, that (as Rocket Hazmat pointed out) you do not need to use the RegExp constructor if you use a regex literal (something without string quotes).
JSFiddle
It does support the {min,max}-syntax, but .match and .test() try to find matching substrings. You will have to include start and end anchors. Also notice that you should either use the RegExp constructor to build a regex from a string or a regex literal, but not both (see MDN: creating regexes).
/^\d{1,7}-\d{2}-\d$/
new RegExp("^\\d{1,7}-\\d{2}-\\d$") // the worse choice
You are constructing your regex incorrectly. Try this (note the anchors, which ensure the string consists of nothing but your pattern):
var pattern= /^\d{1,7}-\d{2}-\d$/;
Otherwise subsets of the existing string will match your regex.
If you need to validate entire input string, use regex pattern
/^\d{1,7}-\d{2}-\d$/
If you need to validate entire line of input string, use regex pattern
/^\d{1,7}-\d{2}-\d$/mg
If you need to find matches within input string, use regex pattern
/(?:\D|^)(\d{1,7}-\d{2}-\d)(?!\d)/g
...and use $1 as a result.
It does support the {n,m} part, the problem here is that your example matches 2345678, so you would need a way of matching the character before the first set of digits