LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-05-2013, 01:51 PM   #1
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 3,391
Blog Entries: 1

Rep: Reputation: 1109Reputation: 1109Reputation: 1109Reputation: 1109Reputation: 1109Reputation: 1109Reputation: 1109Reputation: 1109Reputation: 1109
python 2.7 unable to match against string


Im using python 2.7.4

I have a list of strings. It has a bunch of stuff in it. Most importantly it has this line in it:
Code:
\xc2\xa0- _U
Just an Fyi, if I look at that same line in hex edit though, i get this:
Code:
 - _U
Anyway, I can match part of that string, like this:
Code:
tdstrings = tdstrings.replace('\xc2\xa0-','HELLO')

produces:

HELLO _U
Just as it should.

But this is where i am confused. I add 3 more characters to that statement up there and it doesnt work. Ive used escapes, spaces, unescaped,.. everything i can think of, but basically this example here does not work:
Code:
tdstrings = tdstrings.replace('\xc2\xa0- _U','HELLO')
or
tdstrings = tdstrings.replace('\xc2\xa0- \_U','HELLO')
or 
tdstrings = tdstrings.replace('\xc2\xa0-\ \_U','HELLO')
or
tdstrings = tdstrings.replace('&nbsp-\ \_U','HELLO')
or
tdstrings = tdstrings.replace('\&nbsp-\ \_U','HELLO')

produces:

 - _U
as if none of those match at all. Even though i can match the first part. Someone have a moment to explain this to me?

Last edited by szboardstretcher; 05-05-2013 at 01:56 PM.
 
Old 05-05-2013, 09:29 PM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,758

Rep: Reputation: 644Reputation: 644Reputation: 644Reputation: 644Reputation: 644Reputation: 644
Maybe the space in the input is not actually a space, if you open it with a hex editor what is the actual character - 0x20 ?
 
1 members found this post helpful.
Old 05-05-2013, 10:30 PM   #3
Cacafire
LQ Newbie
 
Registered: May 2013
Location: San Antonio, TX
Distribution: Slackware Linux
Posts: 7

Rep: Reputation: Disabled
Did you try triple quoting?
 
1 members found this post helpful.
Old 05-05-2013, 11:57 PM   #4
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 3,391
Blog Entries: 1

Original Poster
Rep: Reputation: 1109Reputation: 1109Reputation: 1109Reputation: 1109Reputation: 1109Reputation: 1109Reputation: 1109Reputation: 1109Reputation: 1109
Quote:
Originally Posted by kbp View Post
Maybe the space in the input is not actually a space, if you open it with a hex editor what is the actual character - 0x20 ?
Thanks for the reply.

Code:
9312     3e 0a 26 6e 62 73 70 3b  2d 20 5f 55 3c 62 72 3e   >.&nbsp;- _U<br>
Looks like it is a 0x20 space.
 
Old 05-06-2013, 12:02 AM   #5
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 3,391
Blog Entries: 1

Original Poster
Rep: Reputation: 1109Reputation: 1109Reputation: 1109Reputation: 1109Reputation: 1109Reputation: 1109Reputation: 1109Reputation: 1109Reputation: 1109
Quote:
Originally Posted by Cacafire View Post
Did you try triple quoting?
I have now. It works. Thank you much. Have my upvote.

Do you have time to explain why that worked?
 
Old 05-06-2013, 06:54 PM   #6
Cacafire
LQ Newbie
 
Registered: May 2013
Location: San Antonio, TX
Distribution: Slackware Linux
Posts: 7

Rep: Reputation: Disabled
Triple quoting captures everything verbatim.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use Vi to match a string of text, add a new line, insert string... Slycraft Linux - Newbie 7 07-20-2012 11:24 AM
String match in perl user_28 Programming 7 09-16-2010 01:37 PM
reg expr: match a string A-Z, a-z, * or a blank ( but string can not be all blanks) matt007 Programming 4 12-22-2009 09:55 AM
iptables string match htb Linux - Networking 2 08-30-2006 03:37 PM
how to grep only one string pr match gummimann Linux - General 3 11-06-2003 10:40 AM


All times are GMT -5. The time now is 12:38 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration