Goofy
Goofy

Reputation: 6128

Split the string by lower roman after colon and append to HTML

I am trying to split the String dynamically ,My string is as follows:

I need to call 2 numbers: (i) 1234 (ii) 4567 

Sometimes my data might contain like this also:

1st type:

I need to call 2 numbers: numbers are (i) 1234 (ii) 4567 

2nd type:

I need to call my friends: numbers are as follows(only close friends)

i need to dispaly the data like this:

I need to call 2 numbers: 
(i) 1234 
(ii) 4567 

that means after colon get the lower roman (i) and display it in next line, next get the (ii) lower roman and display it in next line and so on.

Now i am using regx to check if my String contains colon(:), if its there then get the pattern matching the regx and display.

It works fine for my initial case but for the 2 types which i have mention it will not work.

This is my code:

if(mString.contains(":")){
                     String [] parts = mString.split (":");

                     html.append("<p>"+parts [0]+"</p>");
                     Matcher m = Pattern.compile ("\\([^)]+\\)[^(]*").matcher (parts [1]);
                     while (m.find ()) {

                         html.append("<p>"+m.group ()+"<br>");
                     }
                     html.append("</br></p>");

EDIT:

I need to call 2 numbers: 
    (i) 1234 // it can be alphabets also like abc
    (ii) 4567 // it can be alphabets also abc

and also the lower romans can be n like (i),(ii),(iii) and so on

Upvotes: 1

Views: 283

Answers (1)

PbxMan
PbxMan

Reputation: 7623

Perhaps this code can help you:

Pattern p = Pattern.compile("^.*:.*([0-9]{4}).*([0-9]{4}).*");
Matcher m = p.matcher("I need to call 2 numbers: (i) 1234 (ii) 4567");
if (m.find()) {
    System.out.println(m.group(1));
    System.out.println(m.group(2));
}

Anyway you could always use a subtring and indexOf,

Upvotes: 3

Related Questions