I want to convert an incoming user string into a regular expression, and the incoming string may contain whitespace. Is there a way to ignore all whitespace within the string?
-
Also, I'm already using Pattern.CASE_INSENSITIVE. Doesn't seem like I can use multiple flags? – robotchicken99 Feb 20 '11 at 04:25
-
You can use multiple flags using the | (single bar) operator. like `Pattern.CASE_INSENSITIVE | Pattern.COMMENTS`. You can see more examples of this at [this link](http://download.oracle.com/javase/tutorial/essential/regex/pattern.html) – Zach L Feb 20 '11 at 04:28
-
Ok thanks, I was using + instead. Bad mistake. – robotchicken99 Feb 20 '11 at 04:29
4 Answers
@Zach L and @smas answered your real question
I want to convert an incoming user string into a regular expression ...
I want to warn you to be cautious. It is easy to write a pathological regex that can consume vast amounts of CPU time for certain input strings. If you allow users to enter arbitrary regexes themselves, there is a risk that they will enter a pathological regex, either by accident, or with the intention of overloading your service ...

- 698,415
- 94
- 811
- 1,216
-
This deserves some attention. We wouldn't want little Bobby Tables coming in! – corsiKa Feb 20 '11 at 07:06
You may be interested in using the COMMENTS flag in the Pattern
class, which:
Permits whitespace and comments in pattern. In this mode, whitespace is ignored, and embedded comments starting with # are ignored until the end of a line.
You can find more info on using flags with Patterns here.
An alternative is to just remove the whitespace before compiling your regex by using either String.replace or String.replaceAll.

- 16,072
- 4
- 38
- 39
Maybe something like this will be good to you:
Pattern p = Pattern.compile("...", Pattern.COMMENTS)
Pattern.COMMENTS means:
Permits whitespace and comments in pattern. In this mode, whitespace is ignored, and embedded comments starting with # are ignored until the end of a line.

- 26,145
- 14
- 53
- 66
Use a regular expression replacement (see http://forums.devx.com/showthread.php?t=145832) to remove the spaces. If you are converting user input to a regular expression, be very careful about escaping (it is like SQL is that users can put in metacharacters that will make your program misbehave); you can get information on proper escaping here.

- 1
- 1

- 30,161
- 7
- 76
- 78