Learner
Learner

Reputation: 161

Regular expression to Find the text between two tabs (java pattens)

I have a text like this

500     Robin Stuart    zzzzzzz

I want to get the text Robin STuart which is enclossed within two tabs. Can someone help me with a regular expression for this. I came up with (^.*?)(\t)(^.*?)(\t) but its not compiling.

Upvotes: 0

Views: 3622

Answers (2)

Pshemo
Pshemo

Reputation: 124265

You are unnecessarily using ^ second time in (^.*?)(\t)(^.*?)(\t) - probably copy-paste mistake. Use:

String s = "500 Robin Stuart    zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz";
Pattern p = Pattern.compile("(\t)(.*?)(\t)");
Matcher m1 = p.matcher(s);
if (m1.find()){
    System.out.println(m1.group(2));
}

Upvotes: 3

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136052

try

    String s = "500\tRobin Stuart\tzzzzzzz";
    s = s.replaceAll(".*\t(.+)\t.*", "$1");
    System.out.println(s);

Upvotes: 0

Related Questions