Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Extracting a string from a URL using regular expression

ziggy25Dec 26 2013

Given the following expression:

"^.*" + "http://www\\.xyz\\.gov/class/"+ "([0-9]+)";

I would expect that if i pass the string "http://www.xyz.gov/class/17950142?dopt=abstract" it should match to everything up to the ? character.

Why does it not do that in the following code snippet?

public class RegexTester {

  private final static String EPR_TYPE_1 = "http://www\\.xyz\\.gov/class/";

  public static void main(String args[]){

  String testRegex = "^.*" + EPR_TYPE_1 + "([0-9]+)";

  String testString = "http://www.xyz.gov/class/17950142?dopt=abstract";

  Pattern PATTERN = Pattern.compile(testRegex, Pattern.CASE_INSENSITIVE);

  Matcher m = PATTERN.matcher(testString);

  if(m.matches()) {

  System.out.println(m.group(1));

  }else{

  System.out.println("No match");

  }

  }

}

I tried the same expression and string in an online regex tester and it seems to work See example here RegExr

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 23 2014
Added on Dec 26 2013
0 comments
1,150 views