Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Patter matching : extract the value between <tr> .....</tr>

Posted on 2010-11-21
14
Medium Priority
?
626 Views
Last Modified: 2013-11-23
Hi,
I want to extract the data between the table row..eg

Input:-

<tr><td>TC_001</td>
<td>DC002</td>

<td><span class="pass">EXCEPTION_DECODE_FAILED</span></td>
<td><span class="ignore">Invalid serial number</span></td>
</tr>
<tr><td>TC_002</td>
<td>DC003</td>
<td><span class="pass">EXCEPTION_NO_SUCH_USER</span></td>
<td><span class="ignore">Invalid GAK or the WCD session has timed out or such WCD user doesn't exist</span></td>
</tr>

serach for <tr>.....</tr>
and get all the data between these tags. i.e each row.
0
Comment
Question by:NishantKashyap
  • 6
  • 4
  • 3
13 Comments
 

Author Comment

by:NishantKashyap
ID: 34185913
I need a regural expression :
this is not working for me:
Pattern.compile("<tr>([a-z,_,0-9,A-Z,\\/,.,>,<]+) /tr>")
0
 
LVL 17

Expert Comment

by:Shinesh Premrajan
ID: 34186030
Pattern.compile("\<tr\>([a-z,_,0-9,A-Z,\\/,.,>,<]+) \/tr\>")

Hope this helps
0
 

Author Comment

by:NishantKashyap
ID: 34186081
I got compiler errors when I used this Pattern.compile("\<tr\>([a-z,_,0-9,A-Z,\\/,.,>,<]+) \/tr\>")


Errors:-
                  Pattern pattern = Pattern.compile("\<tr\>([a-z,_,0-9,A-Z,\\/,.
,>,<]+) \/tr\>") ;
                                                      ^
SourceViewer.java:48: illegal escape character
                  Pattern pattern = Pattern.compile("\<tr\>([a-z,_,0-9,A-Z,\\/,.
,>,<]+) \/tr\>") ;
                                                          ^
SourceViewer.java:48: illegal escape character
                  Pattern pattern = Pattern.compile("\<tr\>([a-z,_,0-9,A-Z,\\/,.
,>,<]+) \/tr\>") ;

         ^
SourceViewer.java:48: illegal escape character
                  Pattern pattern = Pattern.compile("\<tr\>([a-z,_,0-9,A-Z,\\/,.
,>,<]+) \/tr\>") ;

             ^
4 errors
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 17

Expert Comment

by:Shinesh Premrajan
ID: 34186110
Pattern.compile("\\<tr\\>([a-z,_,0-9,A-Z,\\/,.,>,<]+) \\/tr\\>")

Sorry i thought it was a PHP code. IN Java you need to have escape it  using \\ while in PHP there is just  \.

0
 

Author Comment

by:NishantKashyap
ID: 34186224
Thnx but its not working..not able to find that pattern  <tr> ........</tr>
0
 
LVL 17

Expert Comment

by:Shinesh Premrajan
ID: 34186582
Pattern.compile("\\<tr\\>([^\\<tr\\>].*) \\<\\/tr\\>")

How about this
0
 

Author Comment

by:NishantKashyap
ID: 34186729
This expression   Pattern.compile("\\<tr\\>([a-z,_,0-9,A-Z,\\/,.,<,>].*)")
 is able to find

<tr><td>TC_001</td>
<td>DC002</td>


from the below text, but I want till ....</tr>


<tr><td>TC_001</td>
<td>DC002</td>

<td><span class="pass">EXCEPTION_DECODE_FAILED</span></td>
<td><span class="ignore">Invalid serial number</span></td>
</tr>
0
 
LVL 17

Assisted Solution

by:Shinesh Premrajan
Shinesh Premrajan earned 800 total points
ID: 34186755
Pattern.compile("\\<tr\\>([a-z,_,0-9,A-Z,\\/,.,<,>\\s].*)")

If thats a space then need to add that in the pattern

Hope this helps

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34186989
You shouldn't reinvent an html parser using regex - it's neither sound, appropriate or necessary, but the following works for me:
String s = net.proteanit.util.StringUtils.fileToString("tags");
	Pattern p = Pattern.compile("(?msi)<tr>(.*?)</tr>");
	Matcher m = p.matcher(s);
	while(m.find()) {
	    System.out.println(m.group(1));
	}

Open in new window

0
 

Author Comment

by:NishantKashyap
ID: 34187348
thnx CEHJ

this wroks absolutely fine for me


Pattern.compile("\\<tr\\>(\\r*\\s*.*\\r*\\s)*?(\\s*\\<.tr\\>)");
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 1200 total points
ID: 34187545
Please post the code in which you're using that. afaics it doesn't return what you said you wanted it to return
0
 

Author Comment

by:NishantKashyap
ID: 34194137
Ok fine...thnx for the help guys..n I am sorry to close the question in that way.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34195595
:)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question