0

Codes:

  String etr = "fdsfsdaf\nTKT:1101234567890FSDFD";

the format is like ".+\nTKT:\d{13}.+".

how to get first 3 numberic after "\nTKT:" using String.replaceAll to implement? In this example, I want to get is "110". Exclude Matcher,Pattern and Other String method like indexOf(). Because I found the Regex include "?:","?<",but I have tried but failed,something like":

      replaceAll(".+\nTKT:(?=[\\d{3}])\\d{10}.+","");

Thanks in advance.I just know some basic use for Regex.

2
  • So you want to remove 110? Commented Oct 15, 2013 at 6:55
  • no, I want to get 110 Commented Oct 15, 2013 at 6:55

2 Answers 2

1

Or you can replaceAll(".+\nTKT:(\\d{3})\\d{10}.+", "$1");

Sign up to request clarification or add additional context in comments.

1 Comment

upvoted, this answer is answered by Anirudh before.As I want to set the replaceAll result Empty when the regex is not match the input,he modified the answer. Anymore,thank you.
0

Instead of replacing,you should match it

Matcher m=Pattern.compile(".+\nTKT:(\\d{3})\\d{10}.+").matcher(input);
if(m.find())
{
    m.group(1);
}

2 Comments

Hi,Anirudh,thanks for your answer,it is correct,but there has some other problem, if the etr doesn't match the regex, how can let the replaceAll result is EMPTY?Is it can implemented just by modify the Regex?
Hmm,exclude Matcher,Pattern.. though this is an correct answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.