1

I have this input from the user:

*.*.*.*

(* = censor) this string is an input from the user, that's what the user wants to censor. how do I convert it to regex? Edit: Let me explain again. user wants to censor anything in * between the dots, so he types in *.*.*.*, and I need to convert it to regex. he could type text*text too

I have tried doing:

String strLine = "93.38.31.43 and 39.53.19.33 and lala.lala.lala.lala";
String input = "*.*.*.*";
String replaceWord = "[censor]";
input = input.replaceAll("\\*","\\\\\\w+");
strLine = strLine.replaceAll("(?=\\s+)\\S*)" + input + "(?=\\s+)\\S*",replaceWord);

I tried replacing the * with \\w+ to get this output:

the ip's are [censor] and [censor] and [censor]

but It does not work, doesn't replace anything.

when I'm doing it like that, it works:

strLine = strLine.replaceAll("?<=\\s+)\\S*" +"\\w+\\.\\w+\\.\\w+\\.\\w+"+"(?\\s+)\\S*",replaceWord);

why doesn't it work? is there a better way doing it?

5
  • And what is your question? Commented Mar 17, 2015 at 7:27
  • Here's a great tool for testing Java-style regexes (and it provides the Java-escaped string too!) Commented Mar 17, 2015 at 7:30
  • 1
    did you copied your code and pasted it here? because you have a wrong spelling in replaceWord you typed an w instead of an e Commented Mar 17, 2015 at 7:31
  • what does * mean? Is it anything? Is it a digit? Is it anything but space? There are filename globs, but this input is not a file name. You should educate user about proper regular expressions or provide him some kind of constructor for that expressions. *.*.*.* looks like self-invented regular-expression-like language. You should stick to standards. Commented Mar 17, 2015 at 7:37
  • Let me explain. user wants to censor anything between the dots now, so he types in .*.*.*, and I need to convert it to regex. he could type text*text too Commented Mar 17, 2015 at 7:39

1 Answer 1

1

You can use:

String strLine = "93.38.31.43 and 39.53.19.33 and lala.lala.lala.lala";
String input = "*.*.*.*";
String replaceWord = "[censor]";

input = input.replace("*","\\w+").replace(".", "\\.");
System.out.println(input); // \w+\.\w+\.\w+\.\w+

strLine = strLine.replaceAll("\\b" + input + "\\b", replaceWord);
System.out.println(strLine);
//=> [censor] and [censor] and [censor]
Sign up to request clarification or add additional context in comments.

5 Comments

input = input.replace("*","\\w+").replace(".", "\\."); gives me an error.
@Tom An error. Wow. The content of the error message is a secret?
I fixed it adding \\ before the special chars and using replaceAll
Yes I know, weird. I'm using Eclipse and it gives me an error unless I will add \\ before
input.replaceAll("\\*","\\\\\\w+").replaceAll("\\.", "\\\\."); uses regex replacement and input = input.replace("*","\\w+").replace(".", "\\."); just does String replacement. Both should work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.