如何通过特定模式从字符串中提取子字符串

时间:2015-05-13 20:53:39

标签: java

我有一个像DUMMY_CONTENT_DUMMY这样的String对象 _之前或之后的部分实际上是胡言乱语 需要的是两个下划线之间的那个。 有没有办法在java中提取该内容? 也许我必须写一个regex

4 个答案:

答案 0 :(得分:5)

在这种情况下,您不需要正则表达式。

String str = "DUMMY_CONTENT_DUMMY";
String content = str.split("_")[1];

答案 1 :(得分:1)

String x = "AA_BB_CC";    
String[]  arr = x.split("_");  
String middle = arr[1];

此处 中间 包含您的中间部分,即" BB"在这种情况下。

答案 2 :(得分:0)

如果你使用正则表达式,你可以使用前面或后面跟随字符的下划线作为前瞻和后瞻技术的线索。使用Friedl的正则表达式书,我把这个代码作为一个例子。

    /* example method from reference
    *   REF:  Friedel, J. Mastering Regular Expressions.  Ch8: Java.  circa. p.371.
    */

      public static void simpleRegexTest(){

    String myText = "DUMMY_CONTENT_DUMMY";
    String myRegex = "(?<=.*_)(.*)(?=_.*)"; 

    // compile the regex into a pattern that can be used repeatedly
    java.util.regex.Pattern p = java.util.regex.Pattern.compile(myRegex);
    // prepare the pattern to be applied to a given string
    java.util.regex.Matcher m = p.matcher(myText);
    // apply the pattern to the current region and see if the a match is found
    // if will return the first match only
    // while will return matches until there are no more

    while (m.find()){
        // use the match result
        // get the span of text matched with group()
        // in this particular instance, we are interested in group 1
        String matchedText = m.group(1);
        // get the index of the start of the span with start()
        int matchedFrom = m.start();
        // get the index of the end of the span with end()
        int matchedTo = m.end();

        System.out.println("matched [" + matchedText + "] " +
                " from " + matchedFrom +
                " to " + matchedTo + " .");

    } 

当我运行它时,返回的结果是: 匹配[内容]从6到13。

答案 3 :(得分:0)

以下是使用RegEx的方法。

String middlePart = yourString.replaceAll("[^_]*_([^_]*)_[^_]*", "$1");