Results 1 to 2 of 2
  1. #1
    niketanand is offline Member
    Join Date
    Jun 2011
    Posts
    1
    Rep Power
    0

    Default Regular Expression Help

    I want to have regex which shouldn't match if any whitespace, ", } comes just after {
    example-

    1. { abc} should not be matched as it has space after {
    2. {"abc"} should not be matched as it has " just after {
    3. {} and { } should not be matched as it doesn't have anything between {}
    4. {mostRecentAffiliation|textonly='false'|urlonly='t rue'|affiliationtype='school'|linkToForMostRecentA ffiliationTags='member_list'} should be matched
    5. {school|append=' class list'|linkToForAffiliationTags='classlist'} should be matched
    6. <div class=\"color2 text2 customFont txtColor21 floatLeft\">HOTTEST</div>"
    + "<div class=\"floatLeft redArrow\"></div>"
    + "<div class=\"floatLeft txtColor21\">"
    + "<a href=\"{mostRecentAffiliation|textonly='false'|url only='true'|affiliationtype='school'|linkToForMost RecentAffiliationTags='member_list'}\" class=\"link2 txtColor21\"> Check Your Class List</a> | <a href=\"{mostRecentAffiliation|textonly='false'|url only='true'|affiliationtype='school'|linkToForMost RecentAffiliationTags='member_list'}\" class=\"link2 txtColor21\">See Other Profiles</a>"
    + "</div>

    it should pick {mostRecentAffiliation|textonly='false'|urlonly='t rue'|affiliationtype='school'|linkToForMostRecentA ffiliationTags='member_list'} twice

    Please let me know if you can find something to handle this
    I am also trying but somehow something is missing.

    I have junit test case as
    Java Code:
    @Test
      public void checkTagPattern() {
        Pattern tagPattern = Pattern.compile("\\{([^\\}]+)\\}");// \\{([^\\}]+)\\}
        
        String contentWithDuplicateTag = "<div class=\"color2 text2 customFont txtColor21 floatLeft\">HOTTEST</div>"
          + "<div class=\"floatLeft redArrow\"></div>"
          +
    
          "<div class=\"floatLeft txtColor21\">"
          + "<a href=\"{mostRecentAffiliation|textonly='false'|urlonly='true'|affiliationtype='school'|linkToForMostRecentAffiliationTags='member_list'}\" class=\"link2 txtColor21\"> Check Your Class List</a> | <a href=\"{mostRecentAffiliation|textonly='false'|urlonly='true'|affiliationtype='school'|linkToForMostRecentAffiliationTags='member_list'}\" class=\"link2 txtColor21\">See Other Profiles</a>"
          + "</div>";
        String contentString1 = "<style> .hpOverlay{ opacity:0.5; }";
        String contentString2 = "body.ie .hpOverlay{ filter: alpha(opacity=50); }";
        String contentString3 = "<B>{school|append=' class list'|linkToForAffiliationTags='classlist'}</a></b>";
        String contentString4 = "cacheImage : function(indx,cache){\r\n      if(!indx) indx =0;\r\n\t  if(flashBacksMV.cachedImages.length < largeImagesList.length) {\r\n\t    if(!cache) {$(\"#FBCachedImages\").append(\"<img src=\\\"\"+flashBacksMV.nonCachedImages[indx]+\"\\\"  style=\\\"width:534px;height:370px;border:0\\\">\")";
        String contentString5 = "<a href=\"{mostRecentAffiliation|textonly='false'|urlonly='true'|affiliationtype='school'|linkToForMostRecentAffiliationTags='member_list'}\" class=\"link2 txtColor21\"> Check Your Class List</a> | <a href=\"{mostRecentAffiliation|textonly='false'|urlonly='true'|affiliationtype='school'|linkToForMostRecentAffiliationTags='member_list'}\" class=\"link2 txtColor21\">See Other Profiles</a>";
        String contentString6 = "<style> .hpOverlay{}";
        String contentString7 = "<style> .hpOverlay{ }";
    
        Matcher matcher1 = tagPattern.matcher(contentString1);
        Matcher matcher2 = tagPattern.matcher(contentString2);
        Matcher matcher3 = tagPattern.matcher(contentWithDuplicateTag);
        Matcher matcher4 = tagPattern.matcher(contentString3);
        Matcher matcher5 = tagPattern.matcher(contentString4);
        Matcher matcher6 = tagPattern.matcher(contentString5);
        Matcher matcher7 = tagPattern.matcher(contentString6);
        Matcher matcher8 = tagPattern.matcher(contentString7);
        
        assertFalse(matcher1.find());
        assertFalse(matcher2.find());
        assertFalse(matcher7.find());
        assertFalse(matcher8.find());
        assertTrue(matcher3.find());
        assertFalse(matcher5.find());
        assertTrue(matcher4.find());
        assertTrue(matcher6.find());
    
        // while (matcher4.find()) {
        // String group = matcher4.group(1);
        // assertEquals("school|append=' class list'|linkToForAffiliationTags='classlist'",
        // group);
        // }
        // while (matcher6.find()) {
        // String group = matcher6.group(1);
        // assertEquals(
        // "mostRecentAffiliation|textonly='false'|urlonly='true'|affiliationtype='school'|linkToForMostRecentAffiliationTags='member_list'",
        // group);
        // }
      }
    If this test passes then uncomment the bottom code and comment these two lines
    assertTrue(matcher4.find());
    assertTrue(matcher6.find());

    Please help.


    Thanks in advance.
    Niket
    Last edited by Fubarable; 06-23-2011 at 09:44 PM. Reason: code tags added

  2. #2
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    I'm not often using regexes, but when I do I use it to find something, not to tell me that the expression is not in it (and your proposed expression does something like that i.m.h.o: returning the part of the string that is not your expression).
    So maybe you should find strings that contain some of your three sequences and then decide in code not to use them.
    Or alternatively, tell me a regex that reports "not found" when there is "rain" in "grainforest", then I try to give your solution. (I admit I'm no regex-pro.)
    Last edited by Jodokus; 06-24-2011 at 05:05 PM. Reason: removing NO
    No bug ever had to calculate its fitnessfunction.

Similar Threads

  1. regular expression
    By ehsansad in forum New To Java
    Replies: 8
    Last Post: 03-18-2011, 02:23 PM
  2. Help with regular expression
    By mr.ab18 in forum New To Java
    Replies: 2
    Last Post: 08-06-2010, 10:01 PM
  3. regular expression
    By prof.deedee in forum JDBC
    Replies: 3
    Last Post: 02-19-2010, 11:15 AM
  4. regular expression
    By QkrspCmptPop in forum Advanced Java
    Replies: 8
    Last Post: 01-20-2010, 03:55 AM
  5. regular expression
    By ras_pari in forum Advanced Java
    Replies: 27
    Last Post: 10-07-2009, 12:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •