1

I am trying to match everything but garbage values in the entire string.The pattern I am trying to use is:

    ^.*(?!\w|\s|-|\.|[@:,]).*$

I have been testing the pattern on regexPlanet and this seems to be matching the entire string.The input string I was using was:

    Vamsi///#k03@g!!!l.com 123**5

How can I get it to only match everything but the pattern,I would like to replace any string that matches with an empty space or a special charecter of my choice.

2
  • 2
    What do you mean by garbage values? Commented Sep 18, 2013 at 5:30
  • 1
    Your 'pattern' is weird as well. Also, you need to double escape your patterns in Java. Commented Sep 18, 2013 at 5:31

1 Answer 1

8

The pattern, as written, is supposed to match the whole string.

^ - start of string.
.* - zero or more of any character.
(?!\w|\s|-|\.|[@:,]) - negative look-ahead for some characters.
.* - zero or more of any character.
$ - end of string.

If you only want to match characters which aren't one of the supplied characters, try simply:

[^-\w\s.@:,]

[^...] is a negated character class, it will match any characters not supplied in the brackets. See this for more information.

Test.

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

1 Comment

thanks for explaining what I was doing wrong...it worked.I had no idea you could put \s \w . and - in [].

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.