new to java regex, how to grab this part of the string - java

I have a string that looks like:
http://www.example.com/index.do/blah/1_44/asdf/asdf/asdf
http://www.example.com/index.do/blah/1_44_2342/asdf/asdf/asdf
I need to grab the value 44 from the above, ofcourse '44' could be any number.
The number '44' always is prefixed with a _, and after it could be another _ or /.
I have no idea of the java regex API, so as guidance would be appreciated!

It's primarily the Pattern and Matcher classes you're interested in.
String url = "http://www.example.com/index.do/blah/1_44/asdf/asdf/asdf";
Pattern p = Pattern.compile("_(\\d+)");
Matcher m = p.matcher(url);
if (m.find()) {
int number = Integer.valueOf(m.group(1));
}
This pattern finds the first sequence of one or more digits after the first _.

Related

Grouping regular expression

Here is my questions:
I have a very long string with so many values bounded by the different tags. Those values including chinese, english wording and digits.
I wanna to separate by specify pattern. The following is an example:
(I want to find a pattern xxxxxx where xxxx is chinese, english, digits or any notation but not include "<" or ">" as those two symbol is for identify the tags)
However, I found some strange for these pattern. The Pattern seems didn't recgonize the first two tag() but the second one
String a = "<f\"number\">4 <f\"number\"><f$n0>14 <h85><f$n0>4 <f$n0>2 <f$n0>2 7 -<f\"Times-Roman\">7<f\"number\">";
Pattern p = Pattern.compile("<f\"number\">[\\P{sc=Han}*\\p{sc=Han}*[a-z]*[A-Z]*[0-9]*^<>]*<f\"number\">");
Matcher m = p.matcher(a);
while(m.find()){
System.out.println(m.group());
}
The output is as same as my String a
The character class [\\P{sc=Han}*\\p{sc=Han}*[a-z]*[A-Z]*[0-9]*^<>]* matches 0 or more any character because \\P{sc=Han} and \\p{sc=Han} are opposite.
I guess you want:
Pattern p = Pattern.compile("<f\"number\">[\\P{sc=Han}a-zA-Z0-9]*<f\"number\">");
You may want to add spaces:
Pattern p = Pattern.compile("<f\"number\">[\\P{sc=Han}a-zA-Z0-9\s]*<f\"number\">");
or:
Pattern p = Pattern.compile("<f\"number\">[^<]*<f\"number\">");

Regular expression not filtering out digits

Im using this line of code to extract all text between the two strings "Origin" and "//". I'm trying to exclude all digits but this doesn't work, It grabs everything including the digits. is my regex incorrect?
Pattern p = Pattern.compile(Pattern.quote("ORIGIN") + "(.*?[^0-9])" + Pattern.quote("//"), Pattern.DOTALL);
First of all: you have no need to Pattern.quote() either of ORIGIN or //; what is more, the text in your question suggests Origin, not ORIGIN, so I'll go with that instead.
Try this regex:
private static final Pattern PATTERN
= Pattern.compile("Origin([^0-9/]+)//");
Note: it disallows any slash between Origin and // as well, which may, or may not, be what you want; but since there are no examples in your question, this is as good a solution as I can muster.
What you want is not clear.
1) If you want to get only the text (without any number) even if there is number in:
Pattern p = Pattern.compile("ORIGIN(.*)//");
Matcher m = p.matcher(str);
if(m.find())
System.out.println(m.group(1).replaceAll("\\d+", ""));
2) If you want to get text without number :
Pattern p = Pattern.compile("ORIGIN([^0-9]+)//");
Matcher m = p.matcher(str);
if(m.find())
ystem.out.println(m.group(1));
3) Something else ???????
E.g :
String : ORIGINbla54bla//
1) String : blabla
2) No result (Pattern does not match)

Find a subtring in a string using a regular expression

Suppose i have a string kk a.b.cjkmkc jjkocc a.b.c.
I want to find the substring a.b.c in the string , but it is not working.
Here is my code
Pattern p = Pattern.compile("a.b.c");
Matcher m = p.matcher(str);
int x = m.find()
The . in Java Pattern is a special character: "Any character (may or may not match line terminators)" (from the java.util.regex.Pattern web page).
Try escaping it:
Pattern p = Pattern.compile("a\\.b\\.c");
Also note:
Matcher.find returns boolean, not int.
Patterns take double escapes
As others have mentioned, . is a special charater in regular expressions. You can let Java quote sepcial characters using Pattern.quote. BTW: What about String.indexof(String) (which is faster). if you really need regular expressions, have a look at this:
String str = "kk a.b.cjkmkc jjkocc a.b.c.";
Pattern p = Pattern.compile(Pattern.quote("a.b.c"));
Matcher m = p.matcher(str);
while (m.find()) {
int x = m.start();
// ...
}

validate Regular Expression using Java

I need validate a String using a Regular Expression, the String must be like "createRobot(x,y)", where x and y are digits.
I have Something like
String ins;
Pattern ptncreate= Pattern.compile("^createRobot(+\\d,\\d)");
Matcher m = ptncreate.matcher(ins);
System.out.println(m.find());
but doesn't work
Can you help me ?.
Thanks.
You forgot the word Robot in your pattern. Also, parenthesis are special characters in regex, and the + should be placed after the \d, not after a (:
Pattern.compile("^createRobot\\(\\d+,\\d+\\)$")
Note that if you want to validate input that should consist solely of this "createRobot"-string, you mind as well do:
boolean success = s.matches("createRobot\\(\\d+,\\d+\\)");
where s is the String you want to validate. But if you want to retrieve the numbers that were matched, you do need to use a Pattern/Matcher:
Pattern p = Pattern.compile("createRobot\\((\\d+),(\\d+)\\)");
Matcher m = p.matcher("createRobot(12,345)");
if(m.matches()) {
System.out.printf("x=%s, y=%s", m.group(1), m.group(2));
}
As you can see, after calling Matcher.matches() (or Matcher.find()), you can retrieve the nth match-group through group(n).
You must add \ before ( because ( in regex is the special group character
The regexp pattren is:
^create(\d+,\d+)

Java regex doesn't find numbers

I'm trying to parse some text, but for some strange reason, Java regex doesn't work. For example, I've tried:
Pattern p = Pattern.compile("[A-Z][0-9]*,[0-9]*");
Matcher m = p.matcher("H3,4");
and it simply gives No match found exception, when I try to get the numbers m.group(1) and m.group(2). Am I missing something about how Java regex works?
Yes.
You must actually call matches() or find() on the matcher first.
Your regex must actually contain capturing groups
Example:
Pattern p = Pattern.compile("[A-Z](\\d*),(\\d*)");
matcher m = p.matcher("H3,4");
if (m.matches()) {
// use m.group(1), m.group(2) here
}
You also need the parenthesis to specify what is part of each group. I changed the leading part to be anything that's not a digit, 0 or more times. What's in each group is 1 or more digits. So, not * but + instead.
Pattern p = Pattern.compile("[^0-9]*([0-9]+),([0-9]+)");
Matcher m = p.matcher("H3,4");
if (m.matches())
{
String g1 = m.group(1);
String g2 = m.group(2);
}

Categories

Resources