Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get multiple substrings from string in Android/Java ?

Tags:

java

string

regex

I need some help to extract multiple substrings from a string. An example of a string is as given below:

String str = "What is <Mytag a exp 5 exp 3> written as a single power of <i>a</i> <Mytag yx4> and the double power of <b>x+y</b> <Mytag 3xy4>";

I am trying to get substrings Between "<Mytag" and ">"

so my desire output will be
1) a exp 5 exp 3
2) yx4
3) 3xy4

I have tried with Scanner and substring everything I getting first string successfully but the problem with getting second and third occurrence.

in substring methods I am successfully getting index of all tages "<Mytag" but not able to get correct index of ">" because it comes with Bold and italic also.

like image 530
Mac Avatar asked Nov 28 '25 22:11

Mac


1 Answers

Use Regex for that: -

"<Mytag ([^>]*)>"

And get the group 1 from the above regex. You need to use it with Pattern and Matcher class, and use Matcher#find method with a while loop to find all the matching substrings.

like image 62
Rohit Jain Avatar answered Dec 01 '25 11:12

Rohit Jain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!