Solved

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

Posted on 2010-11-21
14
614 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

708 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

16 Experts available now in Live!

Get 1:1 Help Now