开发者

extract substring in java using regex

I need to extract "URPlus1_S2_3" from the string:

"Last one: http://abc.imp/Basic2#URPlus1_S2_3," 

using regular expression in Java language.

Can someone please help me? I am using regex fo开发者_如何学Cr the first time.


Try

Pattern p = Pattern.compile("#([^,]*)");
Matcher m = p.matcher(myString);
if (m.find()) {
  doSomethingWith(m.group(1));  // The matched substring
}


String s = "Last one: http://abc.imp/Basic2#URPlus1_S2_3,";
Matcher m = Pattern.compile("(URPlus1_S2_3)").matcher(s);
if (m.find()) System.out.println(m.group(1));

You gotta learn how to specify your requirements ;)


You haven't really defined what criteria you need to use to find that string, but here is one way to approach based on '#' separator. You can adjust the regex as necessary.

expr: .*#([^,]*)
extract: \1

Go here for syntax documentation:

http://download.oracle.com/javase/1.4.2/docs/api/java/util/regex/Pattern.html


String s = Last one: http://abc.imp/Basic2#URPlus1_S2_3,"
String result = s.replaceAll(".*#", "");

The above returns the full String in case there's no "#". There are better ways using regex, but the best solution here is using no regex. There are classes URL and URI doing the job.


Since it's the first time you use regular expressions I would suggest going another way, which is more understandable for now (until you master regular expressions ;) and it will be easily modified if you will ever need to:

String yourPart = new String().split("#")[1];


Here's a long version:

String url = "http://abc.imp/Basic2#URPlus1_S2_3,";
String anchor = null;
String ps = "#(.+),";
Pattern p = Pattern.compile(ps);
Matcher m = p.matcher(url);
if (m.matches()) {
    anchor = m.group(1);
}

The main point to understand is the use of the parenthesis, they are used to create groups which can be extracted from a pattern. In the Matcher object, the group method will return them in order starting at index 1, while the full match is returned by the index 0.


If you just want everything after the #, use split:

String s = "Last one: http://abc.imp/Basic2#URPlus1_S2_3," ;
System.out.println(s.split("#")[1]);

Alternatively, if you want to parse the URI and get the fragment component you can do:

URI u = new URI("http://abc.imp/Basic2#URPlus1_S2_3,");
System.out.println(u.getFragment());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜