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 +
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) );
At the momemnt I'm using this regular expression to validate address
(!/^[A-Za-z]/i.test(street))
When I use address like this - Esterwergen - it works.
But when I added the sign before the title - 'Esterwergen - it shows my error.
Ho I can modify my RegEx and allow to use this sign before the name?
To allow an optional leading ', you'd change your regexp from
/^[A-Za-z]/
to
/^'?[A-Za-z]/
where the ? means "zero or one times".
If you want to allow the ' anywhere in your string,
/^['A-Za-z]/
would do the trick.
In addition, be sure that you realize that you're only checking the first character of the string as it is.
Right now you will allow Ester9ui4y6ewigkdlLNDSKJ#€=# :::.
To constrain that, you'll need the + quantifier and the $ (end-of-string) anchor.
/^[A-Za-z]+$/
Lets see what is your RegExp targeting:
/^[A-Za-z]/i
^: Asserts position at start of the string.
[]: Match a single character depending on what's inside.
A-Z: Match uppercase letters from A to Z.
a-z: Match lowercase letters from a to z.
i: Case-insensitive.
Consider this:
Using [A-Za-z] along with i flag is redundant. Use /^[a-z]/i or /^[A-z]/ instead.
Using [a-zA-Z\u00C0-\u00FF] for example extends matching to latin characters using UNICODE syntax. See full UNICODE reference here.
Use /^['a-z]+/i to allow ' anywhere in the string.
Use /^'?[a-z]+/i to allow ' only at the beginning of the string. ? means '1 time or 0 times'.
To play around with RegExp you can use tools like this.
"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
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
Having the following regex: ([a-zA-Z0-9//._-]{3,12}[^//._-]) used like pattern="([a-zA-Z0-9/._-]{3,12}[^/._-])" to validate an HTML text input for username, I wonder if is there anyway of telling it to check that the string has only one of the following: ., -, _
By that I mean, that I'm in need of regex that would accomplish the following (if possible)
alex-how => Valid
alex-how. => Not valid, because finishing in .
alex.how => Valid
alex.how-ha => Not valid, contains already a .
alex-how_da => Not valid, contains already a -
The problem with my current regex, is that for some reason, accepts any character at the end of the string that is not ._-, and can't figure it out why.
The other problem, is that it doesn't check to see that it contains only of the allowed special characters.
Any ideas?
Try this one out:
^(?!(.*[.|_|-].*){2})(?!.*[.|_|-]$)[a-zA-Z0-9//._-]{3,12}$
Regexpal link. The regex above allow at max one of ., _ or -.
What you want is one or more strings containing all upper, lower and digit characters
followed by either one or none of the characters in "-", ".", or "_", followed by at least one character:
^[a-zA-Z0-9]+[-|_|\.]{0,1}[a-zA-Z0-9]+$
Hope this will work for you:-
It says starts with characters followed by (-,.,_) and followed and end with characters
^[\w\d]*[-_\.\w\d]*[\w\d]$
Seems to me you want:
^[A-Za-z0-9]+(?:[\._-][A-Za-z0-9]+)?$
Breaking it down:
^: beginning of line
[A-Za-z0-9]+: one or more alphanumeric characters
(?:[\._-][A-Za-z0-9]+)?: (optional, non-captured) one of your allowed special characters followed by one or more alphanumeric characters
$: end of line
It's unclear from your question if you wanted one of your special characters (., -, and _) to be optional or required (e.g., zero-or-one versus exactly-one). If you actually wanted to require one such special character, you would just get rid of the ? at the very end.
Here's a demonstration of this regular expression on your example inputs:
http://rubular.com/r/SQ4aKTIEF6
As for the length requirement (between 3 and 12 characters): This might be a cop-out, but personally I would argue that it would make more sense to validate this by just checking the length property directly in JavaScript, rather than over-complicating the regular expression.
^(?=[a-zA-Z0-9/._-]{3,12}$)[a-zA-Z0-9]+(?:[/._-][a-zA-Z0-9]+)?$
or, as a JavaScript regex literal:
/^(?=[a-zA-Z0-9\/._-]{3,12})[a-zA-Z0-9]+(?:[\/._-][a-zA-Z0-9]+)?$/
The lookahead, (?=[a-zA-Z0-9/._-]{3,12}$), does the overall-length validation.
Then [a-zA-Z0-9]+ ensures that the name starts with at least one non-separator character.
If there is a separator, (?:[/._-][a-zA-Z0-9]+)? ensures that there's at least one non-separator following it.
Note that / has no special meaning in a regex. You only have to escape it if you're using a regex literal (because / is the regex delimiter), and you escape it by prefixing with a backslash, not another forward-slash. And inside a character class, you don't need to escape the dot (.) to make it match a literal dot.
The dot in regex has a special meaning: "any character here".
If you mean a literal dot, you should escape it to tell the regex parser so.
Escape dot in a regex range