If the only prohibited character is the equals sign, something like [^=]* should work.
[^...] is a negated character class; it matches a single character which is any character except one from the list between the square brackets. * repeats the expression zero or more times.
Regex match anything but
Regexp to match everything "except these"
regular expression - Match any character except a specific one at the start of a string, or any whitespace - Unix & Linux Stack Exchange
regex - Match any character except ' - Stack Overflow
If the only prohibited character is the equals sign, something like [^=]* should work.
[^...] is a negated character class; it matches a single character which is any character except one from the list between the square brackets. * repeats the expression zero or more times.
First of all, you don't need a regexp. Simply call contains:
if(str.contains("="))
System.out.println("does not");
else
System.out.println("matches");
The correct regexp you're looking for is just
String patternString = "[^=]*";
Hello all.
Can anyone help me with the below request?
I need a regex condition that matches anything EXCEPT the specified strings. I have tried the following regex, but unfortunately it does not match if I have a string that contains one of the values listed below, e.g. "blue dragon" does not match because it contains "blue". I need it to match if it isn't exactly the values I have in the regex, so if I have "blue dragon" it matches still, although I have "blue" in the regex.
Thanks in advance!
^(?!.*(red|green|blue))
Use
^[^'\n\r]*On Error Goto
Use i case insensitive mode and m multiline mode. See proof.
Explanation
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
[^'\n\r]* any character except: ''', '\n' (newline),
'\r' (carriage return) (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
On Error Goto 'On Error Goto'
Updated link to regex test cases: https://regex101.com/r/UYll0h/6
Since there can be no character when ' isn't there, therefore you will need to use a look-ahead assertion.
Since there can also be other characters of the code (other than ') in the line before on error goto (like in the line if aap = 0 then on error goto Myerrorhandler), to handle those, you will also need to put a condition to check if any character other than ' is present after the look-ahead. This will be done by ([^']+)?.
^(?!')([^']+)?on error goto
The (?) is called a look-ahead. It checks if the characters inside it are present or not. Unlike [], (?) will assert true even when there are no characters. So for example, [a] will check if the first character is 'a', but any expression after it will check from the second character. On the other hand, (?=a) will check if the first character is 'a', and any expression after it will check from the first character. In other words, the look-ahead doesn't move the regex engine to the next character if a match is not found.
I need to replace a server name in a file and the file is in somewhat of a key-value format:
"serverState":"online","server_name":"serverName","ssh_enabled":true
I need to replace serverName with the new name. The problem is, serverName can contain numbers, letters, and special characters (such as "-").
This one works for numbers and letters:
(?:server_name":")(\w|\d)*(?:")
But what if the server name is server-name?
If I try to match any character, it'll include everything past the end quotation, including "ssh_enabled":true, etc:
(?:server_name":").*(?:")
I've tried using a negative look ahead to match any character except ":
(?:server_name":")(?!")*(?:")
But this doesn't match anything.
So how can I match only what's between the quotations for the server_name value, to include letters, numbers, and special characters?
Edit:
Solved:
(?:server_name":")[^"]*(?:")
Hello, I would like to match everything except one particular word (let's say Clide) in a string.
Alice Bob Clide to Alice Clide
The problem is that example I found online don't match the string at all if it contain the word.
I would still like to match what's before and after the word except itself.
Is that possible please?