1

I want to write a regex to match on a string and ignore white spaces. For example, a search for 'foobar' would match on 'foo bar'.

1 Answer 1

4

You can strip the spaces in both the pattern and search strings and use indexOf() if that's acceptable (might be a problem if you don't have enough memory to do so).

I don't think a regex would be a good idea, but you could basically make a pattern like:

/f\s*o\s*o\s*b\s*a\s*r/ 

Which basically has optional whitespaces in between every character.

Sign up to request clarification or add additional context in comments.

6 Comments

Stripping spaces is the way to go. Don't even suggest the regex as an option just because the OP asked for it. :)
@casablanca I said "I don't think a regex would be a good idea," the OP can use it at their own peril ;)
stripping the white spaces is perfect. Thanks!
Maybe "I don't think a regex would be a good idea" should have been in bold... and blinking. :)
@Josh It isn't terrible either, because you won't have to create a new string in memory. Java's String.replace() uses regex anyways.
|

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.