Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2010-11-21
14
Medium Priority
?
624 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

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 …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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 …
Suggested Courses

715 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