0

I have a string contains multi language text as follow

{|en|}Audio Albums{|/en|}{|ar|}الألبومات الصوتية{|/ar|}

I need a function to get the text of specific language

getText("en") should returns Audio Albums

I think I have to use regular expression.

3
  • This sounds more like you want a better String format and use a corresponding parser. For example csv (or dsv) or JSON etc. Commented Oct 28, 2015 at 18:58
  • Possible duplicate of How to split a string in Java Commented Oct 28, 2015 at 19:01
  • Unfortunately, I must working with this format because it comes form API Commented Oct 28, 2015 at 19:02

1 Answer 1

1

This should work:

Pattern pattern = Pattern.compile("\{\|"+lang+"\\|\\}(.*)\\{\\|/"+lang+"\|\}");
Matcher matcher = pattern.matcher(content);
return matcher.find() ? matcher.group(1) : null;
Sign up to request clarification or add additional context in comments.

5 Comments

I tried \\{\\|ar\\|\\}(.*?)\\{\\|/ar\\|\\} regular expression and it returns null
use the find method instead
Added .* at the beginning and end. find() however should work too..., thanks Casimir.
Adding .* is a bad idea and will generate a lot of backtracking for nothing. see the number of steps with and without: regex101.com/r/qF6dC8/1
@Casimir you are right I used find function instead and it worked, I don't need to use .* at the beginning and end

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.