Aman
Aman

Reputation: 838

read a specific word from string and parse the second line from it

I am making a program in java where i am getting from html data from css class .report

@RequestMapping(value = "/medindiaparser",  method = RequestMethod.POST)
public ModelMap  medindiaparser(@RequestParam  String  urlofpage ) throws ClassNotFoundException, IOException  {
    System.out.println("saveMedicineName");
    ModelMap mv = new ModelMap(urlofpage);
    System.out.println();
     String url = urlofpage;
     Document document = Jsoup.connect(url).get();

        String TITLE = document.select(".report").text();
        String[] news = TITLE.split(":");
        System.out.println("Question: " + TITLE);


    return mv;
}

Now what TITLE is giving me.

name : aman kumar working in : home,outside what he does: program | sleep | eat

So what i want to get the specific value in an array like.

array[0] : aman kumar
array[1] : home,outside
array[2] : program | sleep | eat

So that, i can set the value of array in my models, anyone did it?

.report consist <h3> where header is located. it goes like this

<report><h3>Name</h3>aman kumar<h3>working in </h3>home, outside .....</report>

Upvotes: 0

Views: 72

Answers (2)

Darshan Mehta
Darshan Mehta

Reputation: 30809

Try this:

String s = "name : aman kumar working in : home,outside what he does: program | sleep | eat";
String[] news = s.split(":");
String exclude = "(working in|what he does)";
int index = -1;
for(int i = 0 ; i < news.length ; i++){
    if("name".equals(news[i].trim())){
        index = i;
        break;
    }
}
if(index != -1){
    String[] content = Arrays.copyOfRange(news, index+1, news.length);
    for(String string : content){
        System.out.println(string.trim().replaceAll(exclude, ""));
    }
}

Upvotes: 0

Tim Biegeleisen
Tim Biegeleisen

Reputation: 520918

I have completely overhauled my answer to extract the name, working in, and what he does contents from your TITLE string. This can be done using a regex pattern matcher in Java.

String pattern = "name\\s*:\\s*(.*?)\\s*working in\\s*:\\s*(.*?)\\s*what he does\\s*:\\s*(.*)";
Pattern r = Pattern.compile(pattern);
String line = "name : aman kumar working in : home,outside what he does: program | sleep | eat";
Matcher m = r.matcher(line);
while (m.find()) {
    System.out.println(m.group(1));
    System.out.println(m.group(2));
    System.out.println(m.group(3));
}

Output:

aman kumar
home,outside
program | sleep | eat

Demo here:

Rextester

Upvotes: 1

Related Questions