I want to check if a String contains a } with any character in front of it except \.
As far as I know I can use . as a metacharacter in aString.contains(...) to allow any character at that position but I don’t know how to create something like a blacklist: aString.contains(“.(except ‘\‘)}“Is that possible without creating an own method?
You need regex (well technically you don't need regex, but it's the best way):
if (aString.matches(".*(?<!\\\\)}.*"))
This regex says the string should be made up as follows
.* zero or more of any character
(?<!\\\\) the previous char is not a backslash
} a curly right bracket
.* zero or more of any character
This also works for the edge case of the first char being the curly bracket.
See live demo.
Related
I don't have an experience on Regular Expressions. I need to a regular expression which doesn't allow to repeat of special characters (+-*/& etc.)
The string can contain digits, alphanumerics, and special characters.
This should be valid : abc,df
This should be invalid : abc-,df
i will be really appreciated if you can help me ! Thanks for advance.
Two solutions presented so far match a string that is not allowed.
But the tilte is How to prevent..., so I assume that the regex
should match the allowed string. It means that the regex should:
match the whole string if it does not contain 2
consecutive special characters,
not match otherwise.
You can achieve this putting together the following parts:
^ - start of string anchor,
(?!.*[...]{2}) - a negative lookahead for 2 consecutive special
characters (marked here as ...), in any place,
a regex matching the whole (non-empty) string,
$ - end of string anchor.
So the whole regex should be:
^(?!.*[!##$%^&*()\-_+={}[\]|\\;:'",<.>\/?]{2}).+$
Note that within a char class (between [ and ]) a backslash
escaping the following char should be placed before - (if in
the middle of the sequence), closing square bracket,
a backslash itself and / (regex terminator).
Or if you want to apply the regex to individual words (not the whole
string), then the regex should be:
\b(?!\S*[!##$%^&*()\-_+={}[\]|\\;:'",<.>\/?]{2})\S+
[\,\+\-\*\/\&]{2,} Add more characters in the square bracket if you want.
Demo https://regex101.com/r/CBrldL/2
Use the following regex to match the invalid string.
[^A-Za-z0-9]{2,}
[^\w!\s]{2,} This would be a shortest version to match any two consecutive special characters (ignoring space)
If you want to consider space, please use [^\w]{2,}
Need regex for matching { character and every line that ends with this character. For example: (isMoving){
Tried:
^.?\{$
but it works only for single {
.? means zero or one character. You want to match zero or more. Also the curly brace is a special regex character that is used for quantifying the occurrences of a certain pattern, and so it should be escaped:
^.*\{$
The Java string representation of the regex you want is ".*\\{" for use like
if (lineText.matches(".*\\{")) { ... }
A good place to experiment with such things is RegexPlanet's Java page
I have to create a regular expression that validates whether a string contains a forward-slash. If a forward-slash is present the validation fails. The string has to pass even if it is empty.
This is what I have done so far:
"^[a-zA-Z0-9\\\\ ]*$"
You can use a negative lookahead to assert that there cannot be any forward slashes, that looks like (?!.*\/). From there, make sure you find at least one backward slash, or you find nothing (the end of the line):
^(?!.*\/)((?:.*\\.*)|$)
You can see it matching here. Note that there are two matches in the right hand column, one for the empty regex, and one for the line that contains a backward slash.
Edit: If the requirement is only to make sure that the string does not have any forward slashes, then the regex is easier. You just take the negative lookahead from the above regex.
^(?!.*\/).*$
You can see that matching here.
If I understand the comments correctly, then you don't require a backslash for the expression to pass - you just want to make sure there are no forward slashes. In that case, you could simply use ^[^/]*$ You should not need any type of lookahead for this.
To interpret this expression a bit: this expression matches the beginning of the string (^) followed by zero or more non-slash characters ([^/]*), followed by end of string ($). The square brackets usually indicate that you want to match any character inside of them, but in this case, the leading ^ inverts that portion of the expression, so it will match any character that is NOT a slash. The * indicates that we want to attempt that match zero or more times as needed for the pattern to work.
I'm struggling with REGEX and require it for a program.
The input require only alphanumerical keys and also (allow only comma,:,space,/,- in special chars)
I have tried = (^[a-zA-Z0-9,:\S/-]*$)
As far as i understand and please correct me if I'm wrong.
a-zA-Z0-9 - The alphanumerical keys.
,: - Comma and colon
\S - Space
/ - I'm not sure how to represent a forward slash thus i escaped it
- - Dash also not sure if it is needed to escape it.
Would be appreciated if this can be corrected and also a explanation of each part.
Thanks in advance.
You can replace a-zA-Z0-9 with just \\w which is short for [a-zA-Z_0-9]. Furthermore, \\S is any character, but not a whitespace, you should use a \\s instead. You don't need to escape /, and even - if it's the first one or the last one, because if it's placed between two characters it could be interpreted as range and you'll have to escape it. So, you can make your regex like ^([\w,:\s/-]*)$
The \S shorthand matches any character except whitespace, just the opposite of what you want. Lowercase \s matches whitespace [\t\v\n\r\f ]. But if you only want spaces, just put a space in the character class.
a hyphen - needs to be escaped inside characters, unless it's the first or last character in the character class, but you could always escape it just to be sure.
Slashes / don't need to be escaped. They're escaped in other languages where you use them as pattern delimiters. ie: /regex/i.
Besides hyphens and shorthands, only backslashes \\ and closing brackets \] need to be escaped.
Remember in java, you always need to use double backslashes (one is interpreted by java, the other by the regex engine).
Regex
pattern = "^[a-zA-Z0-9 ,:/\\-]*$"
Move the Start of Line ^ and End of Line $ outside the group - like
^([a-zA-Z0-9,:\S/-]*)$
That should do it.
I have comma separated list of regular expressions:
.{8},[0-9],[^0-9A-Za-z ],[A-Z],[a-z]
I have done a split on the comma. Now I'm trying to match this regex against a generated password. The problem is that Pattern.compile does not like square brackets that is not escaped.
Can some please give me a simple function that takes a string like so: [0-9] and returns the escaped string \[0-9\].
For some reason, the above answer didn't work for me. For those like me who come after, here is what I found.
I was expecting a single backslash to escape the bracket, however, you must use two if you have the pattern stored in a string. The first backslash escapes the second one into the string, so that what regex sees is \]. Since regex just sees one backslash, it uses it to escape the square bracket.
\\]
In regex, that will match a single closing square bracket.
If you're trying to match a newline, for example though, you'd only use a single backslash. You're using the string escape pattern to insert a newline character into the string. Regex doesn't see \n - it sees the newline character, and matches that. You need two backslashes because it's not a string escape sequence, it's a regex escape sequence.
You can use Pattern.quote(String).
From the docs:
public static String quote(String s)
Returns a literal pattern String for the specified String.
This method produces a String that can be used to create a Pattern that would match the string s as if it were a literal pattern.
Metacharacters or escape sequences in the input sequence will be given no special meaning.
You can use the \Q and \E special characters...anything between \Q and \E is automatically escaped.
\Q[0-9]\E
Pattern.compile() likes square brackets just fine. If you take the string
".{8},[0-9],[^0-9A-Za-z ],[A-Z],[a-z]"
and split it on commas, you end up with five perfectly valid regexes: the first one matches eight non-line-separator characters, the second matches an ASCII digit, and so on. Unless you really want to match strings like ".{8}" and "[0-9]", I don't see why you would need to escape anything.