extract substring in java using regex

前端 未结 7 1629
走了就别回头了
走了就别回头了 2020-12-10 22:25

I need to extract \"URPlus1_S2_3\" from the string:

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

using regular expressi

相关标签:
7条回答
  • 2020-12-10 23:05
    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 ;)

    0 讨论(0)
  • 2020-12-10 23:06
    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.

    0 讨论(0)
  • 2020-12-10 23:17

    Try

    Pattern p = Pattern.compile("#([^,]*)");
    Matcher m = p.matcher(myString);
    if (m.find()) {
      doSomethingWith(m.group(1));  // The matched substring
    }
    
    0 讨论(0)
  • 2020-12-10 23:19

    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

    0 讨论(0)
  • 2020-12-10 23:25

    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.

    0 讨论(0)
  • 2020-12-10 23:29

    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 讨论(0)
提交回复
热议问题