0

I am reading a file using BufferedReader and I am trying to parse out a string using regex. The string that I would like to parse looks like this,

<test>123</test>

And, there should be text before and end of this string. And, I just want to parse the value of this string (Ex. I want to grab "123" in this case from the read string).

Regex I have is very simple, looks like

<test>?

I guess this is wrong, since it doesn't work. :)

Can somebody show me how to parse a such value from the string using the regex?

Thanks.

2
  • I added the java tag since I think that's the only language with BufferedReader. Commented Jul 17, 2012 at 22:07
  • Does it matter what is before and after "123", or can it be enough that there is text before and end? Commented Jul 17, 2012 at 22:52

2 Answers 2

3

You haven't specified a language, but this should work:

/<test>([^<]+)<\/test>/

The string you are looking for will be in the first captured group.

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

Comments

1

It is not clear what technology you are using. Anyway, a pattern matching the test node is:

<test>(\d*)</test>     //only digits, empty string is mathced

<test>([a-zA-Z0-9\s]*)</test>     //a more general character class

Demo

1 Comment

Ah okay. thanks everyone. Sorry that I wasnt clear. I am using Java and the expression works. thanks everyone.

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.