Java regular expression: Matches back slash character - java

How to macth a backslah (\) in java regular expression? I hava some sript to matching all latex tag In some file but it didnt work.
public class TestMatchTag {
public static void main(String[] args) {
String tag = "\begin";
if (Pattern.matches("\\\\[a-z]+", tag)) {
System.out.println("MATCH");
}
}
}

Replace String tag = "\begin"; with String tag = "\\begin";. The regex is valid, but your input string needs to escape \ character.

Try this,
Pattern.matches("[\\a-z]+", tag)

You need another backslash to escape the "\" in "\begin", change it to "\begin", otherwise the "\b" in your "\begin" will be considered as one character.

This should work...
Pattern.matches("\\[a-z]+", tag);
[a-z] allows any character between a-z more than once and \\ allows "\" once.
you can validate your expression online here

Related

Escape any non-alphanumeric characters in a string using Regex

I would like to escape non-alphanumeric characters occurring in a string as follows:
Say, the original string is: "test_", I would like to transform as "test\_".
In order to do this, one approach I can take by scanning the original string, and constructing a new string and while a non-alphanumeric character is found, append a '\' in front of this character.
But I am wondering if there is any cleaner approach to do the same using regular expression.
You can use the replaceable parameter as shown below:
public class Main {
public static void main(String[] args) {
String s = "test_";
s = s.replaceAll("[^\\p{Alnum}]", "\\\\$0");
System.out.println(s);
}
}
Output:
test\_
Notes:
$0 represents the string matched by the complete regex pattern, [^\\p{Alnum}].
\p{Alnum} specifies alphanumeric character and ^ inside [] is used to negate the pattern. Learn more about patterns from the documentation.
Notice the extra pair of \\ which is to escape \ that has been used to escape \.

Java String replaceAll() method using {} curly brackets

So for my app in Android Studio I want to replace the following:
String card = cards.get(count).getCard();
if (card.contains("{Player1}")) {
String replacedCard = card.replaceAll("{Player1}", "Poep");
}
An example of String card can be: {Player1} switch drinks with the person next to you.
Somehow I can't use {} for the replacing. With the { it says: "Dangling metacharacter". Screenshot: https://prnt.sc/s2bbl8
Is there a solution for this?
the first Argument of replaceAll is a String that is parsed to a regalar Expression (regEx). The braces { } are special reserved meta characters to express something within the regular expression. To match them as normal characters, you need to escape them with a leading backslash \ and because the backslash is also a special character you need to escape itself with an additional backslash:
String replacedCard = card.replaceAll("\\{Player1\\}", "Poep");
Both { } are reserved regex characters. Since the replaceAll() function takes in a regex parameter, you have to explicitly state that { and } are part of your actual string. You can do this by prefixing them with the escape character: \. But because the escape character is also a reserved character, you need to escape it too.
Here's the correct way to write your code:
String card = cards.get(count).getCard();
if (card.contains("{Player1}")) {
String replacedCard = card.replaceAll("\\{Player1\\}", "Poep");
}
You need to escape the initial { with \. I.e;
String card = "{Player1}";
if (card.contains("{Player1}")) {
String replacedCard = card.replaceAll("\\{Player1}", "Poep");
System.out.println("replace: " + replacedCard);
}
The method String.replaceAll expects a regular expression. The other answers already give a solution for this. However, if you don't need regular expressions, then you can also use String.replace:
String replacedCard = card.replace("{Player1}", "Poep");
Since the input value of the replaceAll method expects a regex, you need to escape the curly brackets with a backslash. The curly brackets are special characters in the context of regular expressions.
In Java a backslash in a regex is accomplished by a double backslash \\ (see https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html for reference).
So you would need to adjust the line like so:
String replacedCard = card.replaceAll("\\{Player1\\}", "Poep");
{} are special characters for Regular Expressions. replaceAll method takes as first parameter a Regular Expressions, so if you want also to replace the curly brackets you have to skip them with \\ , as follow:
String card = cards.get(count).getCard();
if (card.contains("{Player1}")) {
String replacedCard = card.replaceAll("\\{Player1}", "Poep");
}

When use java regular-expression pattern.matcher(), source does not match regex.But, my hope result is ,source matches regex

When use java regular-expression pattern.matcher(), source does not match regex.But, my hope result is ,source matches regex.
String source = "ONE.TWO"
String regex = "^ONE\\.TWO\\..*"
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
test();
}
public static void test() {
Test stringDemo = new Test();
stringDemo.testMatcher();
}
public void testMatcher() {
String source = "ONE.TWO";
String regex = "^ONE\\.TWo\\..*";
// The result = false, "not match". But, the hope result is true, "match"
matcher(source, regex);
}
public void matcher(String source, String regex) {
Pattern pattern = Pattern.compile(regex);
boolean match = pattern.matcher(source).matches();
if (match) {
System.out.println("match");
} else {
System.out.println("not match");
}
}
}
In your code, your regular expression expects the o in TWO to be lower case and expects it to be followed by a ..
Try:
String source = "ONE.TWo.";
This will match your regular expression as coded in your question.
The expression \. means match a literal dot (rather than any character). When you code this into a Java String, you have to escape the backslash with another backslash, so it becomes "\\.".
The .* on the end of the expression means "match zero or more of any character (except line-break)".
So this would also match:
String source = "ONE.TWo.blah blah";
Well it doesn't match for two reasons:
Your regex "^ONE\\.TWo\\..*" isn't case sensitive so how do you expect TWo to match TWO.
And your regex expects a . character at the end while your string "ONE.TWO" doesn't have it.
Use the following Regex, to match your source string:
String regex = "^ONE\\.TWO\\.*.*";
Pattern matching is case sensitive by Default. In your case source has a uppercase O and regex a lowercase o.
So you have to add Pattern.CASE_INSENSITIVE or Change the case of o
Pattern pattern = Pattern.compile(regex,Pattern.CASE_INSENSITIVE );
or
String regex = "^ONE\\.TWO\\..*";
Your regex is a bit incorrect. You have an extra dot here:
String regex = "^ONE\.TWO\.(extra dot).*"
Try this one, without dot:
String regex = "^ONE\.TWO.*"
String regex = "^ONE\\.TWO\\..*"
The DOUBLE SLASH \\ in regex is escape sequence to match a SINGLE SLASH \ in Source string.
The .* at the end matches any character 0 or More times except line breaks.
To match the regex your source should be like
String source = "ONE\.TWO\three blah ##$ etc" OR
String source = "ONE\.TWO\.123##$ etc"
Basically its Any String which starts with ONE\.TWO\ and without line breaks.

Java backslash split string

I have a string in the format like "test\00216243".
I need to split the string in Java based on the backslash '\' .
My Program:
public class StringTest {
public static void main(String[] args) {
String taskOwner = "test\00216243";
String taskArray[] = taskOwner.split(Pattern.quote(System.getProperty("file.separator")));
System.out.println(taskArray[0]);
}
}
When i run this program i am getting the following result but not the result as 'test'. Any help?
Result:
test16243
Just to add on
\ is a special character in regular expressions, as well as in Java string literals. If you want a literal backslash in a regex, you have to double it twice.
When you type "\\", this is actually a single backslash (due to escaping special characters in Java Strings).
Regular expressions also use backslash as special character, and you need to escape it with another backslash. So in the end, you need to pass "\\" as pattern to match a single backslash.
public static void main(String[] args) {
String taskOwner = "test\\00216243";
String taskArray[] = taskOwner.split("\\\\");
System.out.println(taskArray[0]);
}
output
test
00216243
\002 represents a unicode character. SO i suggest you to split your input according to the character other than alphabet or digit.
String string1 = "test\00216243";
String part[] = string1.split("[^a-z0-9]");
System.out.println(part[0]);
Output:
test
String "test\00216243" should be replaced with "test\\00216243"
Because \ represent the escape sequence, you need to use \\ in the string to represent a \
Try this
public static void main(String[] args) {
String taskOwner = "test\\00216243";
String myarray[] = taskOwner.split("\\\\");
System.out.println(myarray[0]+" "+myarray[1]);
}
Output
test 00216243
Reference:
How to split a java string at backslash

Matching url with the regex

I take this (?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*\. (?:jpg|gif|png))(?:\?([^#]*))?(?:#(.*))? regular expression from this answer. if i use this in my below program to match the url means i'm getting compiler error .
This is my code:
public static void main(String[] args) {
String url="http://justfuckinggoogleit.com/bart.gif";
matchesImageUrl(url);
}
public static void matchesImageUrl(String url){
Pattern imagePattern=Pattern.compile("(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*\. (?:jpg|gif|png))(?:\?([^#]*))?(?:#(.*))?");
if(imagePattern.matcher(url).matches()){
System.out.println("image matches with the pattern" + url);
}
else{
System.out.println("image does not matches with the pattern");
}
}
You need to escape twice.
So replace \ with \\.
See it work
The problem you have is that the backslash characters () in your regex are escape characters for Java, so it sees \. and \? and thinks your trying to escape a . and a ? - hence the compilation error that you're probably seeing talking about 'Invalid escape characters'.
To fix this, you need to escape the backslashes with their own backslashes. you get:
\\. and \\?
or, in full form:
(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*\\. (?:jpg|gif|png))(?:\\?([^#]*))?(?:#(.*))?

Categories