Solved

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

Posted on 2010-11-21
14
615 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:shinuq
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
 
LVL 17

Expert Comment

by:shinuq
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:shinuq
ID: 34186582
Pattern.compile("\\<tr\\>([^\\<tr\\>].*) \\<\\/tr\\>")

How about this
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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:shinuq
shinuq 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
where is session ID cache stored 1 46
topping1 challenge 7 72
@SBGen Method 3 35
Impossible to extract MSI from new JAVA releases 2 47
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…
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 learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

929 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now