0

I am trying to censor specific strings, and patterns within my application but my matcher doesn't seem to be finding any results when searching for the Pattern.

public String censorString(String s) {
        System.out.println("Censoring... "+ s);
        if (findPatterns(s)) {
            System.out.println("Found pattern");
            for (String censor : foundPatterns) {
                for (int i = 0; i < censor.length(); i++) 
                    s.replace(censor.charAt(i), (char)42);
            }
        }
        return s;
    }

    public boolean findPatterns(String s) {
        for (String censor : censoredWords) {
            Pattern p = Pattern.compile("(.*)["+censor+"](.*)");//regex
            Matcher m = p.matcher(s);
            while (m.find()) {
                foundPatterns.add(censor);
                return true;
            }
        }
        return false;
    }

At the moment I'm focusing on just the one pattern, if the censor is found in the string. I've tried many combinations and none of them seem to return "true".

"(.*)["+censor+"](.*)"
"(.*)["+censor+"]"
"["+censor+"]"
"["+censor+"]+"

Any help would be appreciated.

Usage: My censored words are "hello", "goodbye"

String s = "hello there, today is a fine day."
System.out.println(censorString(s));

is supposed to print " ***** today is a fine day. "

VLAZ
  • 26,331
  • 9
  • 49
  • 67
  • And what is it supposed to do? – MightyPork Feb 01 '14 at 17:31
  • Replace words that are already in my "censoredWords" list, with asterisks. – user3260942 Feb 01 '14 at 17:34
  • hm. [something] means "character class" in regex, I'm not sure why are you using it there. Plus the (.*) look really pointless, I thought you want to match the censored words? – MightyPork Feb 01 '14 at 17:38
  • I've tried many combinations as stated in the post. I'm trying to match them anywhere in the string, and it's not picking them up at all – user3260942 Feb 01 '14 at 17:39
  • What `s.replace(censor.charAt(i), (char)42);` is redundant, unless you use `s = s.replace(censor.charAt(i), (char)42);` – Pshemo Feb 01 '14 at 17:40
  • Oh yeah my mistake, thanks for pointing that out. But I can't even get to that stage yet because my regex is wrong. – user3260942 Feb 01 '14 at 17:41
  • I am not sure if you know that `[]` is [character class](http://www.regular-expressions.info/charclass.html). For instance `[abc]` will not match word `abc` but only one character that is either `a` `b` or `c`. It is very similar to `(a|b|c)`. – Pshemo Feb 01 '14 at 17:44

2 Answers2

0

Your regex is right!!!!. The problem is here.

s.replace(censor.charAt(i), (char)42);

If you expect this line to rewrite the censored parts of your string it will not. Please check the java doc for string.

Please find below the program which will do what you intend to do. I removed your findpattern method and just used the replaceall with regex in String API. Hope this helps.

public class Regex_SO {

private String[] censoredWords = new String[]{"hello"};

 /**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    Regex_SO regex_SO = new Regex_SO();
    regex_SO.censorString("hello there, today is a fine day. hello again");
}

public String censorString(String s) {
    System.out.println("Censoring... "+ s);

    for(String censoredWord : censoredWords){
        String replaceStr = "";
        for(int index = 0; index < censoredWord.length();index++){
            replaceStr = replaceStr + "*";
        }

       s =  s.replaceAll(censoredWord, replaceStr);
    }
    System.out.println("Censored String is .. " + s);
    return s;
}

}

ArunM
  • 2,274
  • 3
  • 25
  • 47
0

Since this seem like homework I cant give you working code, but here are few pointers

  • consider using \\b(word1|word2|word3)\\b regex to find specific words
  • to create char representing * you can write it as '*'. Don't use (char)42 to avoid magic numbers
  • to create new string which will have same length as old string but will be filled with only specific characters you can use String newString = oldString.replaceAll(".","*")
  • to replace on-the-fly founded match with new value you can use appendReplacement and appendTail methods from Matcher class. Here is how code using it should look like

    StringBuffer sb = new StringBuffer();//buffer for string with replaced values
    Pattern p = Pattern.compile(yourRegex); 
    Matcher m = p.matcher(yourText);
    while (m.find()){
        String match = m.group(); //this will represent current match
        String newValue = ...;    //here you need to decide how to replace it
        m.appentReplacemenet(sb, newValue );
    }
    m.appendTail(sb);
    
    String censoredString = sb.toString();
    
Community
  • 1
  • 1
Pshemo
  • 122,468
  • 25
  • 185
  • 269