1

How do I exclude spaces from regex, but will not ignore on special characters. My current code is:-

pattern=re.compile("[^\w]")
var = pattern.sub('', var)

I know \w will remove all whitespace characters, but how to exclude space only?

Example:

var = "hi this is test!@#$%%^^"

after regex it will become:

var = "hi this is test"
6
  • What are the characters you want to strip? Commented Mar 4, 2013 at 4:37
  • 1
    I'm not exactly sure what you are trying to do, but \w matches word characters. Commented Mar 4, 2013 at 4:37
  • im trying to remove !@#$%%^& and etc. without hurting space Commented Mar 4, 2013 at 4:39
  • let me rephrase the question, probably its unclear Commented Mar 4, 2013 at 4:42
  • Are you just trying to remove those characters from the string or possibly others too? Commented Mar 4, 2013 at 4:48

3 Answers 3

5
[^a-zA-Z\d\s]

That removes everything, leaving letters, numbers and spaces.

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

2 Comments

@Natsume \s means not only the blanks but all characters '\f\r\n\t\v\x' too, so if you exclude them from the substitution, they remain in the string. Is it what you want ?
@Natsume - Wouldn't [^\w ] (note the space after the w) accomplish this more succinctly? The space was the only thing that you were missing, unless all of the other whitespace characters mentioned by @eyquem are also intended to be valid.
0

Why not just re.compile('\s')?

2 Comments

nah it removes everything, from var = "this is test !@#" to var = " "
Seems like all this would do would be to remove the whitespace, which is quite the opposite of what Natsume requested. If he replaces all whitespace characters with empty strings, it leaves all of the special characters he wants to remove and removes all of the spaces he wants to keep.
0

If I'm understanding you right, you'd like to remove all characters not a word character or the spaces that separate them. If that is right, then it seems like the following would do the trick:

var = "hi this is test!@#$%%^^"
pattern=re.compile("[^\w ]")
var = pattern.sub('', var)

The only change here is that I've added a space after the ^\w inside the square brackets. When I run this, I get the following result:

>>> print var
hi this is test

Comments

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.