473,750 Members | 2,668 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Matching horizontal white space

multipleSpaces = re.compile(u'\\ h+')

importantTextSt ring = '\n \n \n \t\t '
importantTextSt ring = multipleSpaces. sub("M", importantTextSt ring)

I would have expected consecutive spaces and tabs to be replaced by M
but nothing is being replaced. If I try the following, then I'm left
only with M, as expected -

multipleSpaces = re.compile(u'\\ s+') # both vertical and horizontal

importantTextSt ring = '\n \n \n \t\t '
importantTextSt ring = multipleSpaces. sub("M", importantTextSt ring)
What I eventually wish to do is have only single spaces in my text and
to only have single carriage returns -

" one two three four

five
six

"

becoming -

"one two three four
five
six
"

Thanks,

Barry
Sep 12 '08 #1
3 2358
Ma************* @gmail.com wrote:
multipleSpaces = re.compile(u'\\ h+')

importantTextSt ring = '\n \n \n \t\t '
importantTextSt ring = multipleSpaces. sub("M", importantTextSt ring)
what's "\\h" supposed to mean?
I would have expected consecutive spaces and tabs to be replaced by M
but nothing is being replaced.
if you know what you want to replace, be explicit:
>>importantText String = '\n \n \n \t\t '
re.compile( "[\t ]+").sub("M", importantTextSt ring)
'\nM\nM\nM'

</F>

Sep 12 '08 #2
On Sep 13, 12:52*am, Fredrik Lundh <fred...@python ware.comwrote:
Magnus.Morab... @gmail.com wrote:
multipleSpaces = re.compile(u'\\ h+')
importantTextSt ring = '\n *\n *\n \t\t *'
importantTextSt ring = multipleSpaces. sub("M", importantTextSt ring)

what's "\\h" supposed to mean?
Match *h*orizontal whitespace, I guess ... looks like the maintainer
of the re equivalent in some other language has far too much spare
time :-)

Sep 12 '08 #3
Ma************* @gmail.com writes:
multipleSpaces = re.compile(u'\\ h+')

importantTextSt ring = '\n \n \n \t\t '
importantTextSt ring = multipleSpaces. sub("M", importantTextSt ring)
Please get into the habit of following the Python coding style guide
<URL:http://www.python.org/dev/peps/pep-0008>.

For literal strings that you expect to contain backslashes, it's often
clearer to use the "raw" string syntax:

multiple_spaces = re.compile(ur'\ h+')
I would have expected consecutive spaces and tabs to be replaced by
M
Why, what leads you to expect that? Your regular expression doesn't
specify spaces or tabs. It specifies "the character 'h', one or more
times".

For "space or tab", specify a character class of space and tab:
>>multiple_spac es = re.compile(u'[\t ]+')
important_tex t_string = u'\n \n \n \t\t '
multiple_spac es.sub("M", important_text_ string)
u'\nM\nM\nM'
You probably want to read the documentation for the Python 're' module
<URL:http://www.python.org/doc/lib/module-re>. This is standard
practice when using any unfamiliar module from the standard library.

--
\ “If you do not trust the source do not use this program.” |
`\ —Microsoft Vista security dialogue |
_o__) |
Ben Finney
Sep 14 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
6979
by: gsv2com | last post by:
One of my weaknesses has always been pattern matching. Something I definitely need to study up on and maybe you guys can give me a pointer here. I'm looking to remove all of this code and just use pattern matching to determine if the proper amount of numeric characters has been met. Here is the function I've already done. Any help you can give in a pattern matching solution would be much appreciated and very educational.
7
2226
by: Lachlan Hunt | last post by:
Hi, In the "Firefox 5 Minute Challenge" that I'm trying to write the stylesheet for, the height of language bar needs controlled by the height of the content, so that if the window is not wide enough, and the menu items wrap around to the next line, the height of the <ul> increases, and thus the background stays behind the list items. The height has been set using min-height for for those that support it and 'height' for IE only. It...
21
20650
by: Dan V. | last post by:
I have tried a number of things including preloading, but the background image (water tile in header) in IE 6 will not display where other browsers will. http://www.officeactivate.com/web-site-design.shtml Any ideas? thanks.
2
3823
by: Doug Carter | last post by:
I'm using a horizontal scroll bar control to represent teh default values of MIn 0 to Max 100. I assign the current value of the horizontal control to a label control.Text string but when the horizontal scroll bar reaches the max limit the value indicated is only 91? Changing the big step value to 1 solves the issue but I want to be able to take big steps without writing side effect code to do so. Any suggestions?
3
5394
by: lolo | last post by:
hello. happy new year. I'm trying to build a website for my wife and she is adament on having a horizontal thumbnail scrolling div. great. I have a good vertical scrolling thing, but can't figure out how to make it scroll horizontally. It's probably real simple and I just can't figure it out.... Here is the code I have so far:
2
2418
by: pbd22 | last post by:
Hi. I am trying to make a horizontal list that counts (downward) and a horizontal list that illustrates the time. Both list should be formatted the same (except for the nomenclature). I seem to be successful with the y axis but, in my horizontal list (time), I can't seem to get it to scroll successfully. Below is the link, thanks:
0
3140
by: mjohnson0321 | last post by:
I am trying to incorporate a CSS drop-down menu into a site (suckerfish menu). The menu gets lost behind the content below it, but only on one of the drop downs (News). The error occurs on all of the browsers I have tested it on (IE6, IE7, Firefox, Opera and Safari). It is probably something simple, but I can't find it! Here is the CSS (using external style sheets): style-temp.css (Main style sheet for the site): * { font-family:...
1
2690
by: RFleming | last post by:
Hello, I know this is not directly related to javascript, but I am not awayre of a good group for handling web control questions. I have been trying to get a textbox to work now for over a week, and am starting to think there is not answer. I have a web application that populates an htmlinputtextarea. The reason I chose it was because it could display multiple lines. However, if the report gets too wide for the textbox, the text...
19
3689
by: Jim | last post by:
Hi, I have two questions/problems pertaining to CSS horizontal dropdown menus and am hoping that someone here can help me out. (1) I'm having a problem centering the menu. I picked up the code for this from a tutorial but that menu was flush-left justified. Not what I want. Subsequent searches on google on how to center yielded a
0
9000
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
8838
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9577
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9396
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9256
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4713
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4887
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3322
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2225
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.