473,396 Members | 1,975 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.

convert string to raw string?

Phd
Hi,

I'm writing a regex related program that lets the user supplies the
regex definition. Is there an easy way to convert a string into a raw
string?
Cheers
Jul 18 '05 #1
3 32049

"Phd" <py****@shaw.ca> wrote in message
news:39Usd.439257$nl.135440@pd7tw3no...
Hi,

I'm writing a regex related program that lets the user supplies the regex
definition. Is there an easy way to convert a string into a raw string?


There ain't no such thing as raw strings. Just 'raw' string *literals*,
where 'raw' denotes a mode for converting the literal in the code (which is
*not* a Python string) into a Python string.

Perhaps what you want is something like eval("r'%s'" % (raw_input(),)),
assuming no ' or " in user input.

Terry J. Reedy

Jul 18 '05 #2
Phd wrote:
I'm writing a regex related program that lets the user supplies the
regex definition. Is there an easy way to convert a string into a raw
string?


A raw string is a feature of the syntax of Python. Python gives you several
ways to write strings: single quoted, double quoted, triple single, triple
double, and raw versions of these. Once parsed from the source all of these
simply become objects of type 'str'.

The unicode modifier on a string does produce a different type of object
internally (type 'unicode'), but the raw modifier does not.

So, perhaps you would like to clarify what you really want to do? Perhaps
give an example?

If you are getting input from the user, then unless you are doing some
processing on it to interpret escape sequences the chances are you already
have what you need to use as a regular expression. If you *are*
interpreting escape sequences then the answer is you need to not do that
since the operation isn't really reversible.

Jul 18 '05 #3
Duncan Booth wrote:
If you are getting input from the user, then unless you are doing some
processing on it to interpret escape sequences the chances are you already
have what you need to use as a regular expression. If you *are*
interpreting escape sequences then the answer is you need to not do that
since the operation isn't really reversible.


If you are interpreting escape sequences, you probably want
string.decode or string.encode:
'\\n\\r'.decode('string_escape') '\n\r' '\n\r'.encode('string_escape')

'\\n\\r'

Steve
Jul 18 '05 #4

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

Similar topics

2
by: Joel Moore | last post by:
Maybe I'm just easily baffled after an all-nighter but I can't seem to figure out how to represent a BitArray as a hexadecimal string. For example: Dim outputBank As New BitArray(8) ...
4
by: Eric Lilja | last post by:
Hello, I've made a templated class Option (a child of the abstract base class OptionBase) that stores an option name (in the form someoption=) and the value belonging to that option. The value is...
4
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
3
by: Convert TextBox.Text to Int32 Problem | last post by:
Need a little help here. I saw some related posts, so here goes... I have some textboxes which are designed for the user to enter a integer value. In "old school C" we just used the atoi function...
7
by: patang | last post by:
I want to convert amount to words. Is there any funciton available? Example: $230.30 Two Hundred Thirty Dollars and 30/100
6
by: patang | last post by:
Could someone please tell me where am I supposed to put this code. Actually my project has two forms. I created a new module and have put the following code sent by someone. All the function...
3
by: GM | last post by:
Dear all, Could you all give me some guide on how to convert my big5 string to unicode using python? I already knew that I might use cjkcodecs or python 2.4 but I still don't have idea on what...
4
by: tshad | last post by:
I am trying to convert a string character to an int where the string is all numbers. I tried: int test; string stemp = "5"; test = Convert.ToInt32(stemp); But test is equal to 53.
9
by: Marco Nef | last post by:
Hi there I'm looking for a template class that converts the template argument to a string, so something like the following should work: Convert<float>::Get() == "float"; Convert<3>::Get() ==...
0
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
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
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...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.