Solved

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

Posted on 2010-11-21
14
617 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
14 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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 200 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 300 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to get all the API from website? 11 104
session migration servlets 2 42
Oracle SQL syntax check  without executing 6 75
servlet filter example 37 71
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

856 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