I'm starting with regex in Java recently, and I cant wrap my head around this problem.
Pattern p = Pattern.compile("[^A-Z]+");
Matcher matcher = p.matcher("GETs");
if (matcher.matches()) {
System.out.println("Matched.");
} else {
System.out.println("Did not match.");
}
Result: Did not Match(Unexpected result) Explain this
I get the output "Did not match." This is strange to me, while reading https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html,
I'm using the X+, which matches "One, or more times".
I thought my code in words would go something like this:
"Check if there is one or more characters in the string "GETs" which does not belong in A to Z."
So I'm expecting the following result:
"Yes, there is one character that does not belong to A-Z in "GETs", the regex was a match."
However this is not the case, I'm confused to why this is.
I tried the following:
Pattern p = Pattern.compile("[A-Z]+");
Matcher matcher = p.matcher("GETs");
if (matcher.matches()) {
System.out.println("Matched.");
} else {
System.out.println("Did not match.");
}
Result: Did not match. (Expected result)
Pattern p = Pattern.compile("[A-Z]+");
Matcher matcher = p.matcher("GET");
if (matcher.matches()) {
System.out.println("Matched.");
} else {
System.out.println("Did not match.");
}
Result: Matched. (Expected result)
Please, explain why my first example did not work.
Matcher.matches returns true only if the ENTIRE region
matches the pattern.
For the output you are looking for, use Matches.find instead
Explanation of each case:
Pattern p = Pattern.compile("[^A-Z]+");
Matcher matcher = p.matcher("GETs");
if (matcher.matches()) {
Fails because the ENTIRE region 'GETs' isn't lowercase
Pattern p = Pattern.compile("[A-Z]+");
Matcher matcher = p.matcher("GETs");
if (matcher.matches()) {
This fails because the ENTIRE region 'GETs' isn't uppercase
Pattern p = Pattern.compile("[A-Z]+");
Matcher matcher = p.matcher("GET");
if (matcher.matches()) {
The ENTIRE region 'GET' is uppercase, the pattern matches.
You're very first regex asks to match any character that is not in an uppercase range of A-Z. The match is on the lowercase "s" in GETs.
if you want a regex to match either in UPPERCASE and lowercase, you can use this:
String test = "yes";
String test2= "YEs";
test.matches("(?i).*\\byes\\b.*");
test2.matches("(?i).*\\byes\\b.*");
will return true in the two cases
Related
I have string as follows
"ValueFilter("val1") AND ColumnFilter("val2") AND ValueFilter("val3")"
I have stored the following regex in a array. Using for loop I tried to match the pattern
"ValueFilter\\((.*?)\\)","ColumnFilter\\((.*?)\\)"
what I will do is I will replace the value in the bracket and copy it to a new string.
When I run this above regex against the string in the first loop i have XFilter so it will match both occurrence. But I want to do this in order.
Here is the i thing i want to achieve
first i want to match ValueFilter first then ColumnFilter then again ValueFilter. How can I achieve this?
Edit : Added Code
String expr = "\"ValueFilter(\"val1\") AND ColumnFilter(\"val2\") AND ValueFilter(\"val3\")\"";
String patterns = {"ValueFilter\\((.*?)\\)", "ColumnFilter\\((.*?)\\)"}
for (String pattern : patterns) {
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(expr);
while (m.find()) {
//do something
}
}
Expected Output
ValueFilter("val1")
ColumnFilter("val2")
ValueFilter("val3")
You can use this regex [XY]Filter\((.*?)\) with pattern and you have to loop throw the matches using :
String str = "\"XFilter(\"val1\") AND YFilter(\"val2\") AND XFilter(\"val3\")\"";
String regex = "[XY]Filter\\((.*?)\\)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group());
}
Note you can i use [XY] which mean to match both X or Y,
Output
XFilter("val1")
YFilter("val2")
XFilter("val3")
regex demo
If you want to get only the value you can get the group 1 like matcher.group(1) instead, the output should be :
"val1"
"val2"
"val3"
Edit
what if I have filtername as "ValueFilter" and "ColumnFilter" instead
of X and Y
In this case you can use (Value|Column) instead of [XY] which mean match ValueFilter or ColumnFilter, the regex should look like :
String str = "\"ValueFilter(\"val1\") AND ColumnFilter(\"val2\") AND ValueFilter(\"val3\")\"";
String regex = "(Value|Column)Filter\\((.*?)\\)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group());
}
Output
ValueFilter("val1")
ColumnFilter("val2")
ValueFilter("val3")
Check code demo
Given this String: "a001212a a1212a a00333a a00334a abvbv";
I'm trying to pick the third number just by changing the regex. But I can't figure it out. I am only allowed to change the regex. My code below works if i change the argument to matcher.group(2) but not for group(1).
Help?
String nthNumber(String string) {
Pattern pat = Pattern.compile(
"(\\D+0*(\\d+)\\D+){3,}.*"
);
Matcher matcher = pat.matcher(string);
matcher.matches();
return matcher.group(1);
}
You can make the first group a non-capturing group like below. Then, group(1) should work.
String nthNumber(String string) {
Pattern pat = Pattern.compile(
"(?:\\D+0*(\\d+)\\D+){3,}.*"
);
Matcher matcher = pat.matcher(string);
matcher.matches();
return matcher.group(1);
}
This Java program showing me IndexOutOfBoundsException when it tries to invoke group(1). If I replace 1 with 0 then the whole line is printed.. What do I have to do?
Pattern pattern = Pattern.compile("<abhi> abhinesh </abhi>");
Matcher matcher = pattern.matcher("<abhi> abhinesh </abhi>");
if (matcher.find())
System.out.println(matcher.group(1));
else
System.out.println("Not found");
index starts at 0 so use matcher.group(0)
Edit : To match the text between tag use this regex <abhi>(.*)<\\/abhi>
This post may shed more light on your question.
Confused about Matcher Group.
In short you haven't defined any regular expression grouping to reference an alternate group. You only have the full matching string.
Below if you try adding a grouped regular expression to parse the xml you'll notice 0 has the full string, 1 has the begin tag, 2 has the value, and 3 has the end tag.
Pattern pattern = Pattern.compile("<([a-z]+)>([a-z ]+)</([a-z]+)>");
Matcher matcher = pattern.matcher("<abhi> abhinesh </abhi>");
if (matcher.find()){
System.out.println(matcher.group(0));//<abhi> abhinesh </abhi>
System.out.println(matcher.group(1));//abhi
System.out.println(matcher.group(2));// abhinesh
System.out.println(matcher.group(3));//abhi
}else{
System.out.println("Not found");
}
Try this this regex:
<abhi>(.*)<\\/abhi>
The text you're after will be stored in the first capture group.
Example:
String regex = "<abhi>(.*)<\\/abhi>";
String input = "<abhi>foo</abhi>";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
if (m.find()) {
System.out.println(m.group(1));
}
Take URL http://www.abc.com/alpha/beta/33445566778899/gamma/delta
i need to return the number 33445566778899 (with forward slashes removed, number is of variable length but between 10 & 20 digits)
Simple enough (or so i thought) except everything I've tried doesn't seem to work but why?
Pattern pattern = Pattern.compile("\\/([0-9])\\d{10,20}\\/");
Matcher matcher = pattern.matcher(fullUrl);
if (matcher.find()) {
return matcher.group(1);
}
Try this one-liner:
String number = url.replaceAll(".*/(\\d{10,20})/.*", "$1");
This regex works -
"\\/(\\d{10,20})\\/"
Testing it-
String fullUrl = "http://www.abc.com/alpha/beta/33445566778899/gamma/delta";
Pattern pattern = Pattern.compile("\\/(\\d{10,20})\\/");
Matcher matcher = pattern.matcher(fullUrl);
if (matcher.find()) {
System.out.println(matcher.group(1));
}
OUTPUT - 33445566778899
Try,
String url = "http://www.abc.com/alpha/beta/33445566778899/gamma/delta";
String digitStr = null;
for(String str : url.split("/")){
System.out.println(str);
if(str.matches("[0-9]{10,20}")){
digitStr = str;
break;
}
}
System.out.println(digitStr);
Output:
33445566778899
Instead of saying it "doesn't seem to work", you should have given use what it was returning. Testing it confirmed what I thought: your code would return 3 for this input.
This is simply because your regexp as written will capture a digit following a / and followed by 10 to 20 digits themselves followed by a /.
The regex you want is "/(\\d{10,20})/" (you don't need to escape the /). Below you'll find the code I tested this with.
public static void main(String[] args) {
String src = "http://www.abc.com/alpha/beta/33445566778899/gamma/delta";
Pattern pattern = Pattern.compile("/(\\d{10,20})/");
Matcher matcher = pattern.matcher(src);
if (matcher.find()) {
System.out.println(matcher.group(1));
}
}
I want to break a string like :
String s = "xyz213123kop234430099kpf4532";
into tokens where each token starts with an alphabet and ends with a number. So the above string can be broken down into 3 tokens :
xyz213123
kop234430099
kpf4532
This string s could be very big but the pattern will remain the same, i.e each token will start with 3 alphabets and end with a number.
How do I split them ?
Try this:
\w+?\d+
Java Matcher:
Pattern pattern = Pattern.compile("\\w+?\\d+"); //compiles the pattern we want to use
Matcher matcher = pattern.matcher("xyz213123kop234430099kpf4532"); //we create the matcher on certain string using our pattern
while(matcher.find()) //while the matcher can find the next match
{
System.out.println(matcher.group()); //print it
}
And then you could use Regex.Matches C#:
foreach(Match m in Regex.Matches("xyz213123kop234430099kpf4532", #"\w+?\d+"))
{
Console.WriteLine(m.Value);
}
And for the future this:
RegExr
Do it like this,
String s = "xyz213123kop234430099kpf4532";
Pattern p = Pattern.compile("\\w+?\\d+");
Matcher match = p.matcher(s);
while(match.find()){
System.out.println(match.group());
}
OUTPUT
xyz213123
kop234430099
kpf4532
You can start from such regexp: (\w+?\d+)
http://regexr.com?36utt