Regular Expression Java (punctuations) [duplicate] - java

This question already has answers here:
How can I use "." as the delimiter with String.split() in java [duplicate]
(8 answers)
Closed 9 years ago.
I would like to identify 2 types of patterns in a string. They are:
1) xxx:xxxxxxx:xxx.xx
2) xxx.xxx.xxx.xx:xxxxxxxxxx
Basically I want to know how to identify a literal "." in a string.
Since . means any character, what should I type when I am looking for a literal "."?

You can either escape the . like this \\.
or
use it within character class like this [.]

Try using, String [] stringArray = string.split("\\.");
escaping the "."
And then int periods = stringArray.length;
Telling you how many periods there are in your "String"
Just a start on escaping characters. I am unsure what your question is asking so I just gave an intro to escaping.

good luck
String text ="xxx.xxx.xxx.xx:xxxxxxxxxx";
String patternString = "(.*)(\\.)(.*)";
Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(text);
boolean matches = matcher.matches();
System.out.println(matches);
}

Related

Java Regex match string if the patterns ends with ; or nothing [duplicate]

This question already has answers here:
Alternation operator inside square brackets does not work
(2 answers)
In regex, match either the end of the string or a specific character
(2 answers)
Closed 3 years ago.
[Posting this question because I could not find any question matching my scenario, please point me to the post if this is already discussed, I will delete this post.]
Trying to create a regex to match string app=myApp in long string separated by either , or ;.
My regex fails if the patterns is at end and not terminated by by either , or ;.
This is the regex I have used: [^.][app|APP]=(.*?)[,|;] this works for the following strings:
env=prod;app=myApp;app.secure=yes
app=myApp;app.secure=yes
But does not work for following:
env=prod;app=myApp
app=myApp
Here is my code:
Pattern pattern = Pattern.compile("[^.][app|APP]=(.*?)[,|;]");
Matcher matcher = pattern.matcher(stringVar);
if (matcher.find()) {
return matcher.group(1);
}
I have also tried:
[^.][app|APP]=(.*?)[,|;|$]
but still no luck.
Try Regex: (?:app|APP)=(.*?)(?=,|;|$)
Demo

Regex and Matcher, issue with correctly matching my regex [duplicate]

This question already has answers here:
How do I make part of a regex match optional?
(2 answers)
Closed 4 years ago.
I am reading from a file, and copying the double values within it into an array.
String regEx = "\\s(\\d,??\\d??)";
line = scanner.nextLine();
pattern = Pattern.compile(regEx);
matcher = pattern.matcher(line);
if(matcher.find()) {
System.out.println(matcher.group(1));
grades[i++] = Double.parseDouble(matcher.group(1));
}
But this only seems to copy the whole number part from the file, not the part after the ".", the fraction. It appears to completely ignore the part of the regex that is quantified by "??". I assume it is a problem with my regex, I cannot figure out what is wrong tho-
Okay the regular expression I was looking for is "\\s(\\d(\\.\\d)?)"

converting RegEx into my Java function [duplicate]

This question already has answers here:
Why does this Java regex cause "illegal escape character" errors?
(7 answers)
Closed 2 years ago.
I'm having problems with Java RegEx. That's my regex statement "\"730\"\s+{([^}]+)}" and it works on an regex checking website, but I have trouble getting it to work in Java. That's my current code.
String patternString = '\"730\"\s+{([^}]+)}';
Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(vdfContentsString);
boolean matches = matcher.matches();
Thanks for advice.
It says "Illegal escape character in character literal".
Single quotes (') declare characters, double quotes (") declare strings, that's why you get the syntax error Illegal escape character in character literal. Second, regex itself syntactically uses the backslash, as in \s for whitespace. Maybe confusing might be the fact that Java also uses \ for character escaping. That's why you need two backslashes (\\s in Java will become \s for the resulting regular expression).
Then you need to take care of special characters in regular expressions: { and } are quantifiers ("repeat n times"), if you want them literally, escape them (\\{ and \\})
So if you want to match a string like "730" {whatever}, use this regular expression:
"730"\s+\{([^}]+)\}
or in Java:
String patternString = "\"730\"\\s+\\{([^}]+)\\}";
Example:
String str = "\"730\" { \"installdir\" \"C:\\Program Files (x86)\\Steam\\steamapps\\common\\Counter-Strike Global Offensive\" \"HasAllLocalContent\" \"1\" \"UpToDate\" \"1\" }";
String patternString = "\"730\"\\s+\\{([^}]+)\\}";
System.out.println(str.matches(patternString)); // true
Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal repetition
Escape { and } as well because in Java Regex Pattern it has special meaning.
String patternString = "\"730\"\\s+\\{([^\\}]+)\\}";
EDIT
String#matches() method looks for whole string if you are looking for sub-string of a long string then use Matcher#find() method and get the result from the groups that is captured by enclosing the pattern inside parenthisis (...).
sample code:
String patternString = "(\"730\"\\s+\\{([^\\}]+)\\})";
Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(vdfContentsString);
while (matcher.find()) {
System.out.println(matcher.group(1));
}
{, } are Metacharacters (See HERE for metacharacters) and need to be escaped with \\, hence, \\{ .. \\}.
\ is an escape character, while \s, \w, \d etc (See HERE for a list) are metacharacters, therefore, as mentioned above, these need to be escaped as well, hence, \\s+
instead of [^\\}], i would suggest (.+?)}
This is working:
String patternString = '\\\"730\\\"\\s+\\{(.+?)\\}';
The above is the required Java string which gets parsed into the following regular expression: \"730\"\s+\{(.+?)\}, and then it can be used to match the input string. Tadan!
two levels of parsing!

String.split won't let me split with periods [duplicate]

This question already has answers here:
How can I use "." as the delimiter with String.split() in java [duplicate]
(8 answers)
Closed 9 years ago.
I am trying to do a String.split on a website address using the "." so that I can find the domain name of the website.
However, when I do this:
String href = "www.google.com";
String split[] = href.split(".");
int splitLength = split.length;
It tells me that the splitLength variable is 0. Why is this, and how can I make this work?
Try using this to split the string:
href.split("\\.");
Explanation: split splits on a regex, not on a regular substring. In regexes, . is the metacharacter for 'match any character', which we don't want. So we have to escape it using a backslash \. But \ is also a metacharacter for escaping in Java strings, so we need to escape it twice.
Split uses a regex so do:
String split[] = href.split("\\.");

Replace all with a string having regex wild chars [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
java String.replaceAll without regex
I have a string and I need to replace some parts of it.
The replacement text contains regex wild chars though. Example:
String target = "Something * to do in ('AAA', 'BBB')";
String replacement = "Hello";
String originalText = "ABCDEFHGIJKLMN" + target + "ABCDEFHGIJKLMN";
System.out.println(originalText.replaceAll(target, replacement));
I get:
ABCDEFHGIJKLMNSomething * to do in ('AAA', 'BBB')ABCDEFHGIJKLMN
Why doesn't the replacement occur?
Because *, ( and ) are all meta-characters in regular expressions. Hence all of them need to be escaped. It looks like Java has a convenient method for this:
java.util.regex.Pattern.quote(target)
However, the better option might be, to just not use the regex-using replaceAll function but simply replace. Then you do not need to escape anything.
String.replaceAll() takes a regular expression and so it's trying to expand these metacharacters.
One approach is to escape these chars (e.g. \*).
Another would be to do the replacement yourself by using String.indexOf() and finding the start of the contained string. indexOf() doesn't take a regexp but rather a normal string.

Categories