0

I am going to remove the first index space and last index spaces via python's re feature:

I tried:

    re.sub(r"\s+" ,""," hello world ") // remove the first place space

But it does not removed any thing.

7
  • @VigneshKalai I want to use re specially. Commented Jul 10, 2015 at 18:56
  • /^\s|\s$/g regex101.com/r/iQ6eE8/1 Commented Jul 10, 2015 at 19:00
  • @VigneshKalai The question title says it ...with regular expression Commented Jul 10, 2015 at 19:00
  • It removes every space Commented Jul 10, 2015 at 19:02
  • 1
    What version of Python do you have - my copy of Python 2.7.3 gives 'helloworld' in response to re.sub(r"\s+", "", " hello world "). While that doesn't exactly match your stated goal (which would be better achieved with strip(), as already mentioned), it does in fact remove the spaces from the string... Commented Jul 10, 2015 at 19:18

1 Answer 1

1
>>> re.sub(r'^\s+|\s+$', '', ' hello world ')
'hello world'

That will remove all leading and trailing whitespaces, though, not necessarily only the first and last index.

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

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.