Hi, im working with the Java Regex class and so im parsing some text. each piece of text is in a separate line. here's a sample of the text:

this is textHello world
buzzinga!
Makmende says hi
Makmende has poked u. dnt try poking him back!
young, forever young
another one from ozeki
Sample text message
Hello world
Hello world
gfxchgfj gh jkgjkg
Oya! Jikakamueni bwana.
Oya!
Yes!
important Yellow
21 M 23415 GOOD
20 F 51433 BAD
23 F 31532 TRUE
20 M 54143 BAD


im interested in the last 4 texts(highlighted) and any other text thats in that format. My regex for sorting this text is "(\\d{2})\\s([MF])\\s(\\d{5})\\s([a-zA-Z0-9]*)"

My code for working it out is:

String smstext = txtsmslist.getText();
        String smsformat = "(\\d{2})\\s([MF])\\s(\\d{5})\\s([a-zA-Z0-9]*)";
        
        Pattern pattern = Pattern.compile(smsformat);//pattern has the regex used for searching
        //pattern.compile(smsformat, Pattern.MULTILINE);
        Matcher matcher =pattern.matcher(smstext); //matcher is the test text to search through

        if (matcher.find()){
            System.out.println("Found value: "+ matcher.group(0));
            txtparselist.setText(matcher.group(0));

        }else{
            JOptionPane.showMessageDialog(null,"No Matching texts","No Match",JOptionPane.INFORMATION_MESSAGE);
            System.out.println("NO MATCH");
        }

using this code, i have managed to pick out the first entry i.e 20 F 51433 BAD only but i need to match all of the remaining similar text i.e ny

20 F 51433 BAD
23 F 31532 TRUE
20 M 54143 BAD

and any other text in that format that i may be added to this list.

PS: the text above is in a textpane and not in a file. i only wish to put the output text to a file.


Someone please help me out.

Recommended Answers

All 6 Replies

Hi, im working with the Java Regex class and so im parsing some text. each piece of text is in a separate line. here's a sample of the text:

this is textHello world
buzzinga!
Makmende says hi
Makmende has poked u. dnt try poking him back!
young, forever young
another one from ozeki
Sample text message
Hello world
Hello world
gfxchgfj gh jkgjkg
Oya! Jikakamueni bwana.
Oya!
Yes!
important Yellow
21 M 23415 GOOD
20 F 51433 BAD
23 F 31532 TRUE
20 M 54143 BAD


im interested in the last 4 texts(highlighted) and any other text thats in that format. My regex for sorting this text is "(\\d{2})\\s([MF])\\s(\\d{5})\\s([a-zA-Z0-9]*)"

My code for working it out is:

String smstext = txtsmslist.getText();
        String smsformat = "(\\d{2})\\s([MF])\\s(\\d{5})\\s([a-zA-Z0-9]*)";
        
        Pattern pattern = Pattern.compile(smsformat);//pattern has the regex used for searching
        //pattern.compile(smsformat, Pattern.MULTILINE);
        Matcher matcher =pattern.matcher(smstext); //matcher is the test text to search through

        if (matcher.find()){
            System.out.println("Found value: "+ matcher.group(0));
            txtparselist.setText(matcher.group(0));

        }else{
            JOptionPane.showMessageDialog(null,"No Matching texts","No Match",JOptionPane.INFORMATION_MESSAGE);
            System.out.println("NO MATCH");
        }

using this code, i have managed to pick out the first entry i.e 20 F 51433 BAD only but i need to match all of the remaining similar text i.e ny

20 F 51433 BAD
23 F 31532 TRUE
20 M 54143 BAD

and any other text in that format that i may be added to this list.

PS: the text above is in a textpane and not in a file. i only wish to put the output text to a file.


Someone please help me out.

Matcher.find() is a boolean function, so when you use it in an if statement it only runs once. You want to run a loop that goes until it doesn't find a match. This isn't necessarily great code, but most likely something like this:

int count = 0;

while (matcher.find()) {
            count++;
            System.out.println("Found value: "+ matcher.group(0));
            txtparselist.setText(matcher.group(0));
}
if (count == 0) {
            JOptionPane.showMessageDialog(null,"No Matching texts","No Match",JOptionPane.INFORMATION_MESSAGE);
            System.out.println("NO MATCH");
}

Thanx Leverin4. It does output all the relevant text. However, there is that bit where the output is displayed in a textpane i.e

txtparselist.setText(matcher.group(0));

it only displays the last entry i.e

20 M 54143 BAD

how do i display all of them in the textpane?

Accumulate the text inserting newlines or use an append() method to add to the end.

Thanx Leverin4. It does output all the relevant text. However, there is that bit where the output is displayed in a textpane i.e

txtparselist.setText(matcher.group(0));

it only displays the last entry i.e

20 M 54143 BAD

how do i display all of them in the textpane?

Yes, exactly as Norm said. Use a temporary string variable to store this and set the text at the very end. I apologize for missing this before. Something like this:

int count = 0;
String result = "";
       
while (matcher.find()) {
     count++;
     System.out.println("Found value: "+ matcher.group(0));
     result = result + matcher.group(0) + "\n";
}
if (count == 0) {
     JOptionPane.showMessageDialog(null,"No Matching texts","No Match",JOptionPane.INFORMATION_MESSAGE);
     System.out.println("NO MATCH");
} else {
     txtparselist.setText(result)
}

Wonderful! It worked. thanks a lot guys.

No problem. Norm's other method was to use the text pane's .append method. That's another thing you may want to look in to.

You mind marking the thread as solved?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.