简体   繁体   中英

Get the index of matched string with regex

I am trying to fetch the content between the tags. So i made regex for the same.

    final String REGEX_BOLD_END = ".*[<][/][B|b][>].*";
    String input = "<B>Contetnt here</B>";
    Pattern pattern_start = Pattern.compile(".*[<][B|b][>].*");
    Matcher matcher_start = pattern_start.matcher(input);
    Pattern pattern_end = Pattern.compile(REGEX_BOLD_END);
    Matcher matcher_end = pattern_end.matcher(input);
    System.out.println("Tag open");
    if (matcher_start.matches()) {
        System.out.println("At:" + matcher_start.start() + "\tTo:" + matcher_start.end());
        System.out.println(matcher_start.group(0));
    } else {
        System.out.println("Not matched");
    }
    System.out.println("Tag Close");
    if (matcher_end.matches()) {
        System.out.print("At:" + matcher_end.start() + "\tTo:" + matcher_end.end());
    } else {
        System.out.println("Not matched");
    }

My aim is to get the Content here . So i was thinking to get the start and end index and then fetch the substring out of the original input. But i am getting something what i was not expecting.

output:

Tag open

At:0    To:20
<B>Contetnt here</B>
Tag Close
At:0    To:20

Please point out where i am making mistake.

If you're thinking of using substring in relation to Regex'es, you're doing it wrong. The whole point of regular expressions is to not bother with indexes or substring.

Try this instead:

Pattern p = Pattern.compile("<[b|B]>(.*)</[b|B]>");
Matcher m = p.matcher(textToMatch);
if (m.find()) {
    String firstMatch = m.group(1);
}

Edit : Complete, compiling command line program, which outputs "Yay!" when input is "<b>yay!</b>" as per requirement.

import java.util.regex.*;
class Test {
    public static void main(String[] args) {
        Pattern p = Pattern.compile("<[b|B]>(.*)</[b|B]>");
        Matcher m = p.matcher(args[0]);
        if (m.find()) {
            System.out.println(m.group(1));
        }
        else System.out.println("No match");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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