By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,907 Members | 1,832 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,907 IT Pros & Developers. It's quick & easy.

How to find the first space?

P: n/a
How can I find the first space using regex?

For example I have text
Text=' This is a sample '

The last space I can remove by
Text=re.sub(r"\s(?!\w)",'',Text)

but I do not know how to remove the first space.
Can anyone help?

Thanks
L.
Jun 27 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"Johny" <py****@hope.czwrote in message
news:7a**********************************@m44g2000 hsc.googlegroups.com...
How can I find the first space using regex?

For example I have text
Text=' This is a sample '
Why do you need to use a regex?

text = text.replace(" ", "")

Russ

Jun 27 '08 #2

P: n/a
Russell Blau wrote:
"Johny" <py****@hope.czwrote in message
news:7a**********************************@m44g2000 hsc.googlegroups.com...
>How can I find the first space using regex?

For example I have text
Text=' This is a sample '

Why do you need to use a regex?

text = text.replace(" ", "")
You are aware that this is producing nonsense given the OP's requirements?
>>print Text.replace(" ", "")
Thisisasample

Instead, the OP wants
>>print Text.strip()
This is a sample

Diez

Jun 27 '08 #3

P: n/a
Johny wrote:
How can I find the first space using regex?

For example I have text
Text=' This is a sample '

The last space I can remove by
Text=re.sub(r"\s(?!\w)",'',Text)

but I do not know how to remove the first space.
Can anyone help?
Use the strip-method, as defined on stringlike objects.

Diez
Jun 27 '08 #4

P: n/a
Le Monday 09 June 2008 17:02:51 Johny, vous avez écrit*:
How can I find the first space using regex?

For example I have text
Text=' This is a sample '

The last space I can remove by
Text=re.sub(r"\s(?!\w)",'',Text)
For spaces at the end of line, I prefer this one :

re.sub(r'\s*$', '', s)
but I do not know how to remove the first space.
Can anyone help?
At the beginning :

re.sub(r'^\s*', '', s)

And the one that strip the whole line (not obvious) :

re.sub(r'\s*(\S+.*\S+)\s*', r'\1', s)
You really should consider s.strip(), rstrip(), lstrip()

String manipulation methods cover almost all use cases, and give better codes,
keep regexes for what they are really valuable (parsing complex idioms,
second step optimisation).

I can't even remember the last time I used re module in production code in
python (unlike in perl or shell).
>
Thanks
L.
--
http://mail.python.org/mailman/listinfo/python-list


--
_____________

Maric Michaud
_____________

Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 4 26 88 00 97
Mobile: +33 6 32 77 00 21
Jun 27 '08 #5

P: n/a
On Jun 9, 5:02*pm, Johny <pyt...@hope.czwrote:
How can I find the first space using regex?

For example I have text
Text=' This is a sample '

The last space I can remove by
Text=re.sub(r"\s(?!\w)",'',Text)

but I do not know how to remove the first space.
Can anyone help?

Thanks
L.
If it's leading spaces you're worried about you can use the .lstrip()
method, if you want to find the first space you can use .index(' ') to
get the position of it and do with what you want.
Jun 27 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.