-
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
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
-
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.)