简体   繁体   English

使用正则表达式提取Java中的子字符串

[英]extract substring in java using regex

I need to extract "URPlus1_S2_3" from the string: 我需要从字符串中提取"URPlus1_S2_3"

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

using regular expression in Java language. 使用Java语言中的正则表达式。

Can someone please help me? 有人可以帮帮我吗? I am using regex for 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 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 "#". 如果没有“#”,则上面的代码将返回完整的String。 There are better ways using regex, but the best solution here is using no regex. 使用正则表达式有更好的方法,但是最好的解决方案是不使用正则表达式。 There are classes URL and URI doing the job. 有类URL和URI来完成这项工作。

因为这是您第一次使用正则表达式,所以我建议您采用另一种方式,这种方式现在更容易理解(直到您掌握正则表达式;为止),并且如果需要,可以很容易地对其进行修改:

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. Matcher对象中, group方法将从索引1开始按顺序返回它们,而完全匹配由索引0返回。

If you just want everything after the # , use split: 如果只需要#之后的所有内容,请使用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并获取片段组件,则可以执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM