-3

I have the following String

"Sample"

however in some occurences there may be one or more spaces in the beginning:

" Sample" or " Sample"

Without using trim, what regular expression can I use to match if there is one? I am using (\s+) for the current String. I want the space to be included as part of the same one group.

Rolando
  • 58,640
  • 98
  • 266
  • 407
  • 5
    Why would you not just use trim? This is going to come across as a personal attack, but I want to state that I don't mean it that way. It's become a fairly common pattern for people to post very simple questions but with ridiculous restrictions. In my opinion, if you're going to say without using trim, you should provide a *very* good reason for not using trim. – Corbin Mar 27 '12 at 20:42
  • Why not use `strip`? In any case, `\s*` matches whitespace in a Regex – Niklas B. Mar 27 '12 at 20:42
  • Not a personal attack, I know you can trim, but it would be convenient if I didn't have to trim. – Rolando Mar 27 '12 at 20:44
  • 2
    So you already use a regex? please include it in the question. – Niklas B. Mar 27 '12 at 20:46
  • This question may be of use to you: http://stackoverflow.com/questions/3873361/finding-multiple-occurrences-of-a-string-within-a-string-in-python – arboc7 Mar 27 '12 at 20:47
  • 4
    **What is your real goal with this?** Are you trying to remove the spaces? Are you trying to replace them with something else? Are you just trying to know where in the string they can be found? Do you just want to know if any exist at all? There is a separate answer for each of those possibilities. – Platinum Azure Mar 27 '12 at 20:50

3 Answers3

1

You don't need regular expressions:

s = " Sample"
if s[0] == ' ':
    # do something
else:
    # do something else

But if you want regex:

import re
s = ' Sample'
if re.match(r"\s+.+", s):
   # found space at beginning, do something
alan
  • 4,752
  • 21
  • 30
  • The regex should be something like `\s+` or `^\s`. – Niklas B. Mar 28 '12 at 13:45
  • OP said 'there may be one or more spaces in the beginning', not there *will be*, hence the '*'. But you're right, I did add the '^'. – alan Mar 28 '12 at 14:29
  • Right, the question is not clear. I thought he wanted to know *whether* there was spaces at the beginning, in which case that regex wouldn't be helpful, but it's not clear that this is what OP wants. If you use `re.match`, you don't need the `^`, by the way. – Niklas B. Mar 28 '12 at 14:31
  • Yes, I see your point. It's not really clear. Also, I'm not sure what he's really trying to do. – alan Mar 28 '12 at 14:32
1
"   Sample".lstrip()

The reason that lstrip is prefereable is that there are any number of hidden (or unprintable) characters. To get a regular expressions to capture all the purmutations is a little messy display wise. Clear code is the best code.

There is the option of using POSIX character classes within your regex, however the re module does not seem to support them at this time. You will have to use a different module that supports the POSIX classes (regex 0.1.20120323).

(Note: Newly registered so I could not 'post more than 2 hyperlinks'.)

References:

  • docs.python.org/library/string.html "If chars is omitted or None, whitespace characters are removed."

  • stackoverflow.com/questions/9897375/how-to-match-a-space-at-the-beginning-of-the-expression-python "POSIX character classes ... [:space:] \s \s [ \t\r\n\v\f] Whitespace characters"

  • pypi.python.org/pypi/regex "POSIX character classes are supported. This is actually treated as an alternative form of \p{...}."

  • http://pypi.python.org/pypi/regex "Alternative regular expression module, to replace re."

J. Kuhl
  • 39
  • 2
0

Use find():

s = " Sample"
pos = 0
if s.find(' ') == pos:
    print 'Space found!'

You can then increment pos until you no longer find a space.

arboc7
  • 5,762
  • 2
  • 27
  • 30