Home arrow static arrow Java Programming [Archive] - newbie in regex... pb with pattern
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - newbie in regex... pb with pattern
This topic has 8 replies on 1 page.

Posts:13
Registered: 1/16/04
newbie in regex... pb with pattern  
Jun 15, 2004 6:38 AM



 
Hello,

I want to identify each occurence of -> <tr bgcolor="#f7f7f7"> <- in a html code source, this string could be written like this -> <td class="t2" bgcolor="#f7f7f7"> <-...

I try the fallowing java code to parse it :

Pattern p=Pattern.compile("<tr bgcolor=\"#f7f7f7\">"); <** CORRET ;-) **>
or
Pattern p=Pattern.compile("<td[ class=\"t2\"]? bgcolor=\"#f7f7f7\">"); <** FALSE **>
or
Pattern p=Pattern.compile("<td[ class=\"t2\"]?? bgcolor=\"#f7f7f7\">"); <** FALSE **>
or
Pattern p=Pattern.compile("<td[ class=\"t2\"]?+ bgcolor=\"#f7f7f7\">"); <** FALSE **>

which one do I have to use ??

thx

stuuf
 

Posts:24,036
Registered: 2/3/03
Re: newbie in regex... pb with pattern  
Jun 15, 2004 6:56 AM (reply 1 of 8)



 
String test = "<td class=\"t2\" bgcolor=\"#f7f7f7\">";String pattern = "<td.*?bgcolor=\"#f7f7f7\">";System.out.println(test.matches(pattern)); // true

But, I question the usefulness of such a thing...
 

Posts:24,036
Registered: 2/3/03
Re: newbie in regex... pb with pattern  
Jun 15, 2004 6:57 AM (reply 2 of 8)



 
Wait a minute - what "occurence" are you trying to identify? The background wiki/./color attribute? The <tr> tag? The <td> tag?
 

Posts:3,055
Registered: 18/06/98
Re: newbie in regex... pb with pattern  
Jun 15, 2004 7:01 AM (reply 3 of 8)



 
import java.util.regex.*; public class Test {    public static void main(String[] args) {        String[] s = {            "<tr bgcolor=\"#f7f7f7\">", //-- will be replaced            "<html></html>", //-- will not be replaced, the pattern is not here            "<TR BGCOLOR=\"#f7f7f7\">", //-- will not be replaced, pattern is case-sensitive            "<html><body><table><tr bgcolor=\"#f7f7f7\"></table></body></html>" //-- please insert more examples here        };        for (int i = 0; i < s.length; ++i) {            System.out.println (s[i].replaceAll("<tr bgcolor=\"#f7f7f7\">", "<td class=\"t2\" bgcolor=\"#f7f7f7\">"));        }    }}


Post more examples - I really can't figure exactly what you want. For instance, do you need to replace <tr> and <TR> by <td>? Do you need to replace any color (not just #f7f7f7)?
 

Posts:13
Registered: 1/16/04
Re: newbie in regex... pb with pattern  
Jun 15, 2004 7:19 AM (reply 4 of 8)



 
I want to parse and study the html code write in a page after each repetition of -> <tr bgcolor="#f7f7f7"> <-, BUT this <tr> tag is somtime written like that -> <td class="t2" bgcolor="#f7f7f7"> <- .
So I have to not identify -> class="t2" <- strictfully...

But I found a solution like that :
Pattern p=Pattern.compile("<tr[ class=\"t2\"]* bgcolor=\"#f7f7f7\">");

I had the same pb with this tag : <td class="t2Lmn" align="center" valign="middle"> where -> valign="middle"<- could appear or not. And my solution seem to work properly like that :
Pattern p=Pattern.compile("<td class=\"t2Lmn\" align=\"center\"[ valign=\"middle\">]*");

So... I read the other response and I continue...

thx !

 

Posts:24,036
Registered: 2/3/03
Re: newbie in regex... pb with pattern  
Jun 15, 2004 7:35 AM (reply 5 of 8)



 
A <tr> tag can never be "written like" <td>. They're different tags with different meanings. As you work with regular expressions, you'll find that it helps to define your criteria VERY specifically. There are "better" ways to write your expressions if you can properly define what you're trying to do.

For instance, if you just want to find tags with that particular bgcolor attribute, you can just use something like
<.*?bgcolor=\"#f7f7f7\">

Good luck!
 

Posts:24,036
Registered: 2/3/03
Re: newbie in regex... pb with pattern  
Jun 15, 2004 7:36 AM (reply 6 of 8)



 
Correction:
<.*?bgcolor=\"#f7f7f7\".*?>
This will allow for other attributes after bgcolor...
 

Posts:24,036
Registered: 2/3/03
Re: newbie in regex... pb with pattern  
Jun 15, 2004 7:36 AM (reply 7 of 8)



 
... but may not help you if you use JSTL and nested tags... Ah well, you get the idea, hopefully.
 

Posts:2,391
Registered: 9/26/00
Re: newbie in regex... pb with pattern  
Jun 15, 2004 9:00 AM (reply 8 of 8)



 
In a regex, square brackets are used to create a character class, which means "match any one of this set of characters". You're using a character class to match a specific sequence of characters, which is incorrect. It happens to work in this case, but you should really use grouping (parentheses) for that. For instance, if you only wanted to match tags of the form
  <tr bgcolor="#f7f7f7"> or <tr class="t2" bgcolor="#f7f7f7">
but not, e.g.,
  <tr class="t1" bgcolor="#f7f7f7"> or <tr bgcolor="#f7f7f7" class="t2">
you could put the class="t2" part in a group and make the group optional, like so:
  Pattern p = Pattern.compile("<tr (class=\"t2\" )?bgcolor=\"#f7f7f7\">");

For a correct use of character classes, suppose you want to match either tr or td tags with the bgcolor="#f7f7f7" attribute (and you don't care about any other attributes). This slight variation on yawmark's solution would do the trick:
  Pattern p = Pattern.compile("<t[dr] .*?bgcolor=\"#f7f7f7\".*?>");

If you want to learn more about regexes, here's a good place to start: http://www.regular-expressions.info/
 
This topic has 8 replies on 1 page.