I was experimenting trying to extract the 't' and 'f' flags from here.
So I was surprised to see extra characters in the output. Apparently the matcher backtracked - I dont understand why. What should be the correct regex?
System.out.println("searching...");
// "Sun:\\s Mon:\\s Tue:\\s Wed:\\s Thu:\\s Fri:\\s Sat:\\s "
Pattern p = Pattern.compile("[t|f]");
Matcher m = p.matcher("Sun:t Mon:f Tue:t Wed:t Thu:f Fri:t Sat:f ");
while (m.find()) {
System.out.println(m.group());
}
Output:
searching...
t
f
t
t
f
t
t
f
Sat has a t in it. Try ":([tf])" instead.
Pattern p = Pattern.compile(":([tf])");
Matcher m = p.matcher("Sun:t Mon:f Tue:t Wed:t Thu:f Fri:t Sat:f ");
while (m.find()) {
System.out.println(m.group(1));
}
Related
I have this string:
text=123+456+789&xxxxxxxxx&yyyyyyyyyy&zzzzzzzzzzz
I need to extract 123+456+789
What I done so far is:
String s = "text=123+456+789&xxxxxxxxx&yyyyyyyyyy&zzzzzzzzzzz";
String ps = "text=(.*)&";
Pattern p = Pattern.compile(ps);
Matcher m = p.matcher(s);
if (m.find()){
System.out.println(m.group(0));
System.out.println(m.group(1));
}
And I got all text until the last & which is: 123+456+789&xxxxxxxxx&yyyyyyyyyy while the requested output is: 123+456+789
Any suggestions how to fix it (regex is mandatory)?
Use a negated character class:
String ps = "text=([^&]*)";
The value you need will be in Group 1.
The [^&] matches any character but an ampersand.
You almost getting, you need to make your regex lazy (or non greedy) like this:
String ps = "text=(.*?)&";
here ---^
Working demo
Try this regex :
([0-9+]+)
Link : https://regex101.com/r/xU2zF4/1
java code :
String s = "text=123+456+789&xxxxxxxxx&yyyyyyyyyy&zzzzzzzzzzz";
String ps = "([0-9+]+)";
Pattern p = Pattern.compile(ps);
Matcher m = p.matcher(s);
if (m.find()){
System.out.println(m.group(0)); // value of s
System.out.println(m.group(1)); // returns 123+456+789
}
I'm trying to get the last return code from an SSH shell in linux.
I'm using the command:echo &? to get it.
I've written following code but it's not working:
int last_len = 0;
Pattern p = Pattern.compile("echo $?\r\n[0-9]");
while(in.available() > 0 ) {
last_len = in.read(buffer);
String str = new String(buffer, 0, last_len);
Matcher m = p.matcher(str);
if(m.find()) {
return Integer.parseInt(m.group().substring(9));
}
}
What am I doing wrong?
You need to escape $, ? in the regex inorder to match the literal form of those characters since ?, $ are considered as special chars in regex.
Pattern p = Pattern.compile("echo \\$\\?\\r?\\n([0-9])");
Matcher m = p.matcher(str);
if(m.find()) {
System.out.println(m.group(1));
}
or
Pattern p = Pattern.compile("echo\\s+\\$\\?[\\r\\n]+([0-9])");
I'm trying to extract a string from round brackets.
Let's say, I have John Doe (123456789) and I want to output the string 123456789 only.
I have found this link and this regex:
/\(([^)]+)\)/g
However, I wasn't able to figure out how to get the wanted result.
Any help would be appreciated. Thanks!
String str="John Doe (123456789)";
System.out.println(str.substring(str.indexOf("(")+1,str.indexOf(")")));
Here I'm performing string operations. I'm not that much familiar with regex.
this works for me :
#Test
public void myTest() {
String test = "test (mytest)";
Pattern p = Pattern.compile("\\((.*?)\\)");
Matcher m = p.matcher(test);
while(m.find()) {
assertEquals("mytest", m.group(1));
}
}
You need to escape brackets in your regexp:
String in = "John Doe (123456789)";
Pattern p = Pattern.compile("\\((\\d*)\\)");
Matcher m = p.matcher(in);
while (m.find()) {
System.out.println(m.group(1));
}
In Java, you need to use
String pattern = "\\(([^()]+)\\)";
Then, the value you need is in .group(1).
String str = "John Doe (123456789)";
String rx = "\\(([^()]+)\\)";
Pattern ptrn = Pattern.compile(rx);
Matcher m = ptrn.matcher(str);
while (m.find()) {
System.out.println(m.group(1));
}
See IDEONE demo
I have sentence and I want to calculate words, semiPunctuation and endPunctuation in it.
Command "m.group()" will show String result. But how to know which group is found?
I can use method with "group null", but it is sounds not good.
String input = "Some text! Some example text."
int wordCount=0;
int semiPunctuation=0;
int endPunctuation=0;
Pattern pattern = Pattern.compile( "([\\w]+) | ([,;:\\-\"\']) | ([!\\?\\.]+)" );
Matcher m = pattern.matcher(input);
while (m.find()) {
// need more correct method
if(m.group(1)!=null) wordCount++;
if(m.group(2)!=null) semiPunctuation++;
if(m.group(3)!=null) endPunctuation++;
}
You could use named groups to capture the expressions
Pattern pattern = Pattern.compile( "(?<words>\\w+)|(?<semi>[,;:\\-\"'])|(?<end>[!?.])" );
Matcher m = pattern.matcher(input);
while (m.find()) {
if (m.group("words") != null) {
wordCount++;
}
...
}
String line = "asdasdasdasd <meta name=\"generator\" content=\"WordPress 3.5.2\" /> asdasdasdasdasd";
Pattern p = Pattern.compile("<meta name=\"generator\" content=\"WordPress\\s+([\\d.]+)\" />");
Matcher m = p.matcher(line);
if(m.matches())
System.out.println(m.group(1));
else
System.out.println("not found");
The regex I have used does not give the desired result. I want the wordpress version from the supplied string.
Matcher#matches() matches at the beginning of the string. So, you would need to build regex for complete string.
Alternatively, you can use Matcher#find() with just the regex for relevant part of the string:
Pattern p = Pattern.compile("content=\"WordPress\\s+([\\d.]+)\"");
Matcher m = p.matcher(line);
if(m.find())
System.out.println(m.group(1));
else
System.out.println("not found");
You have to escape the dot and accept more numbers just in case
Pattern p = Pattern.compile("WordPress\\s+([\\d+\\.]+)");