regex problem using Pattern class

Hi everybody,

I am using the Pattern class to create a pattern which has to match "Sanofi-Aventis" (without the quotes). The pattern is created like this:

Pattern p = Pattern.compile("(?i)(" + Pattern.quote(match) +")");

It is then matched as follows:

Matcher m = p.matcher(retval);

String retval = m.replaceAll("<font color=\"#CCCCCC\"><strong>$1</strong></font>");

I have printend the value of "match" and of "Pattern.quote(match)", and they look like this:

[quote]

match: Sanofi-Aventis

Pattern.quote(): \QSanofi-Aventis\E

[/quote]

However, "Sanofi-Aventis" does not get matches in the text. Words / Strings without a "-" work fine, so I guess I am doing something wrong with respect to the dash. Can anybody help me out?

Best regards,

Jethro

[930 byte] By [Jethroa] at [2007-11-27 5:45:32]
# 1
That can't be your code because it would require two definitions of 'retval' . Post a short self-contained example that we can work with.
sabre150a at 2007-7-12 15:27:23 > top of Java-index,Java Essentials,Java Programming...
# 2

This is my entire code:

public static String colorOccurences(String p_text, ArrayList<String> p_matches, String p_HTMLColorCode)

{

String retval = p_text;

// Color the matches.

for (String match : p_matches)

{

Pattern p = Pattern.compile("(?i)(" + Pattern.quote(match) + ")");

Matcher m = p.matcher(retval);

retval = m.replaceAll("<font color=\"" + p_HTMLColorCode + "\"><strong>$1</strong></font>");

}

return retval;

}

I also fixed my problem: the "p_matches" array also contained the String "Sanofi", this shorter String was matched first. I will look into this.

Best regards,

Jethro

Jethroa at 2007-7-12 15:27:23 > top of Java-index,Java Essentials,Java Programming...
# 3
I'm not sure, but shouldn't "(?i)" this be a non-capturing group "(?:i)"?
quittea at 2007-7-12 15:27:23 > top of Java-index,Java Essentials,Java Programming...
# 4
> I'm not sure, but shouldn't "(?i)" this be a> non-capturing group "(?:i)"?This the embedded case-insensitive flag - not a group.
sabre150a at 2007-7-12 15:27:23 > top of Java-index,Java Essentials,Java Programming...
# 5
> > I'm not sure, but shouldn't "(?i)" this be a> > non-capturing group "(?:i)"?> > This the embedded case-insensitive flag - not a group.Edit: sorry, forget it. I just looked it up
quittea at 2007-7-12 15:27:23 > top of Java-index,Java Essentials,Java Programming...
# 6

Hi there,

I now sort the p_matches ArrayList on String length to make sure I find the longest possible match. Besides that I am now using the CASE_INSENSITIVE flag instead of "(?i)". This is my final code:

public static String colorOccurences(String p_text, ArrayList<String> p_matches, String p_HTMLColorCode)

{

String retval = p_text;

// Sort the matches ArrayList on String length, starting with the longest String.

Collections.sort(p_matches, new StringLengthComparator());

// Color the matches.

for (String match : p_matches)

{

Pattern p = Pattern.compile("(" + Pattern.quote(match) + ")", Pattern.CASE_INSENSITIVE);

Matcher m = p.matcher(retval);

retval = m.replaceAll("<font color=\"" + p_HTMLColorCode + "\"><strong>$1</strong></font>");

}

return retval;

}

/**

* Comparator object for Strings. Orders by length of String from large to small,

* then alphabetically.

*/

private static class StringLengthComparator implements Comparator

{

public int compare(Object x,Object y)

{

if(x instanceof String && y instanceof String)

{

String xstr = (String) x, ystr = (String) y;

if(xstr.length() == ystr.length()) return xstr.compareTo(ystr);

else if(xstr.length() < ystr.length()) return 1;

else return -1;

}

else return 0;

}

}

Best regards,

Jethro

Jethroa at 2007-7-12 15:27:23 > top of Java-index,Java Essentials,Java Programming...