JAVA REGEX - Split a string with "?" as delimiter - java

I want to split a string with "?" as the delimiter.
str.split("?")[0] fails.

The argument to the "split" method must be a regular expression, and the '?' character has special meaning in regular expressions so you have to escape it. That's done by adding a backslash before it in the regexp. However, since the regexp is being supplied by way of a Java String, it requires two backslashes instead so as to get an actual backslash character into the regexp:
str.split( "\\?" )[0];

str.split("\\?")[0]

Related

How to put [] in my regex [duplicate]

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.

Werid behavior - Java regex expression taken from DB

I have regex expression stored in DB - '\\\\E\\\\', I use java to fetch it and match it to Strings.
I thought that since java reads from the DB it knows to escape SQL special characters by itself, and all I need is to escape the regex special chracters, so this expression actually matches '\\E\\'.
Problem is that it macthes '\E\' rather then '\\E\\' , why?
If you want to use a regex to match one literal backslash character, you need to use four backslashes in a Java string.
The regex \\ matches one literal backslash.
The string "\\" denotes a single backslash.
Therefore, in order to build a regex that consists of two backslashes, you need a Java string with four backslashes.
So you need "\\\\\\\\E\\\\\\\\" to construct a regex that matches \\E\\...

PatternSyntaxException when splitting string at "*" character

Every time i try to split the string "hello*world" using s.split("*"); I get a PatternSyntaxException.
I have tried using s.split("\*"); but that gives me another error. Im sure this is something simple.
How do i stop this?
* is a meta-character in regular expressions used as a wildcard quantifier to match zero of more characters
Try using 2 backslash characters
s.split("\\*");
The split method takes a regular expression as the argument, not a normal string. The * has special meaning in regular expressions. If you want to split on a literal *, you have to escape it with a backslash. But the backslash is also an escape character in Java string literals, so you have to escape the backslash too by using two backslashes:
s.split("\\*")

Android '\' special character

I have an android application where I have to find out if my user entered the special character '\' on a string. But i'm not obtaining success by using the string.replaceAll() method, because Java recognizes \ as the end of the string, instead of the " closing tag. Does anyone have suggestions of how can I fix this?
Here is an example of how I tried to do this:
private void ReplaceSpecial(String text) {
if (text.trim().length() > 0) {
text = text.replaceAll("\", "%5C");
}
It does not work because Java doesn't allow me. Any suggestions?
Try this: You have to use escape character '\'
text = text.replaceAll("\\\\", "%5C");
Try
text = text.replaceAll("\\\\", "%5C");
replaceAll uses regex syntax where \ is special character, so you need to escape it. To do it you need to pass \\ to regex engine but to create string representing regex \\ you need to write it as "\\\\" (\ is also special character in String and requires another escaping for each \)
To avoid this regex mess you can just use replace which is working on literals
text = text.replace("\\", "%5C");
The first parameter to replaceAll is interpreted as a regular expression, so you actually need four backslashes:
text = text.replaceAll("\\\\", "%5C");
four backslashes in a string literal means two backslashes in the actual String, which in turn represents a regular expression that matches a single backslash character.
Alternatively, use replace instead of replaceAll, as recommended by Pshemo, which treats its first argument as a literal string instead of a regex.
text = text.replaceAll("\", "%5C");
Should be:
text = text.replaceAll("\\\\", "%5C");
Why?
Since the backward slash is an escape character. If you want to represent a real backslash, you should use double \ (\\)
Now the first argument of replaceAll is a regular expression. So you need to escape this too! (Which will end up with 4 backslashes).
Alternatively you can use replace which doesn't expect a regex, so you can do:
text = text.replace("\\", "%5C");
First, since "\" is the escape character in Java, you need to use two backslashes to get one backslash. Second, since the replaceAll() method takes a regular expression as a parameter, you will need to escape THAT backslash as well. Thus you need to escape it by using
text = text.replaceAll("\\\\", "%5C");
I could be late but not the least.
Add \\\\ following regex to enable \.
Sample regex:
private val specialCharacters = "-#%\\[\\}+'!/#$^?:;,\\(\"\\)~`.*=&\\{>\\]<_\\\\"
private val PATTERN_SPECIAL_CHARACTER = "^(?=.*[$specialCharacters]).{1,20}$"
Hope it helps.

Removing literal character in regex

I have the following string
\Qpipe,name=office1\E
And I am using a simplified regex library that doesn't support the \Q and \E.
I tried removing them
s.replaceAll("\\Q", "").replaceAll("\\E", "")
However, I get the error Caused by: java.util.regex.PatternSyntaxException: Illegal/unsupported escape sequence near index 1
\E
^
Any ideas?
\ is the special escape character in both Java string and regex engine. To pass a literal \ to the regex engine you need to have \\\\ in the Java string. So try:
s.replaceAll("\\\\Q", "").replaceAll("\\\\E", "")
Alternatively and a simpler way would be to use the replace method which takes string and not regex:
s.replace("\\Q", "").replace("\\E", "")
Use the Pattern.quote() function to escape special characters in regex for example
s.replaceAll(Pattern.quote("\Q"), "")
replaceAll takes a regular expression string. Instead, just use replace which takes a literal string. So myRegexString.replace("\\Q", "").replace("\\E", "").
But that still leaves you with the problem of quoting special regex characters for your simplified regex library.
String.replaceAll() takes a regular expression as parameter, so you need to escape your backslash twice:
s.replaceAll("\\\Q", "").replaceAll("\\\\E", "");
You can also use the below. I used this because i was matching and replacing a text wrapped and the Q & E would stay in the pattern. This way it doesn't.
final int flags = Pattern.LITERAL;
regex = "My regex";
pattern = Pattern.compile( regex, flags );

Categories