473,396 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to find the first space?

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
5 3462
"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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: hype | last post by:
Hi, How can I find out how much space has been used in the log file and how much of it is free or yet to be used ? Thanks, Hype
4
by: Vic | last post by:
Hi, With VBA code from MSAccess, how to search for a string in another text file, and replace it with space and then save it under the same name? Your help is greatly appreciated. Rgds
2
by: John Regan | last post by:
Hello All I am trying to find the owner of a file or folder on our network (Windows 2000 Server) using VB.Net and/or API. so I can search for Folders that don't follow our company's specified...
2
by: alacrite | last post by:
With this testprogram: #include <iostream> #include <string> using namespace std; int main() {
14
by: inpuarg | last post by:
I want to find a & character using Regex. But not && How can i manage this in c# Quickfind window ? -------------------------------------------------- ne kadar yaşarsan yaşa sevdiğin kadardır...
3
by: Amritha.Datta | last post by:
Can anyone tell me why the below code returns error? Exception Details: System.IO.FileNotFoundException: Could not find file Dim strLFolder As String = "c:\Temp\F Files" Dim intClientID As...
0
by: sndive | last post by:
I have a weid problem. If i do this: import elementtree.ElementTree as ET .... tree = ET.parse("whatever") root = tree.getroot() r = root.find('last') print r return root where last is not an...
22
by: Steve Richter | last post by:
Does the .NET framework provide a class which will find the item in the collection with a key which is closest ( greater than or equal, less than or equal ) to the keys of the collection? ex:...
9
by: xiao | last post by:
It always dumped when I tried to run it... But it compiles OK. What I want to do is to do a test: Read information from a .dat file and then write it to another file. The original DAT file is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.