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

regular expressions

P: n/a
hi i am looking for pattern in regular expreesion that replaces
anything starting with and betweeen http:// until /
like http://www.start.com/startservice/yellow/ fdhttp://helo/abcd will
be replaced as
p/startservice/yellow/ fdp/abcd

Nov 6 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Nov 6, 4:49 pm, krishnamanenia...@gmail.com wrote:
hi i am looking for pattern in regular expreesion that replaces
anything starting with and betweeen http:// until /
likehttp://www.start.com/startservice/yellow/fdhttp://helo/abcdwill
be replaced as
p/startservice/yellow/ fdp/abcd
What have you come up with so far? Have you looked at the 're' module?

--
Paul Hankin

Nov 6 '07 #2

P: n/a
On Tue, Nov 06, 2007 at 08:49:33AM -0800, kr***************@gmail.com wrote regarding regular expressions:
>
hi i am looking for pattern in regular expreesion that replaces
anything starting with and betweeen http:// until /
like http://www.start.com/startservice/yellow/ fdhttp://helo/abcd will
be replaced as
p/startservice/yellow/ fdp/abcd
You don't need regular expressions to do that. Look into the methods that strings have. Look at slicing. Look at len. Keep your code readable for future generations.

Py>>help(str)
Py>>dir(str)
Py>>help(str.startswith)

Cheers,
Cliff
Nov 6 '07 #3

P: n/a
On Nov 6, 11:07 am, "J. Clifford Dyer" <j...@sdf.lonestar.orgwrote:
On Tue, Nov 06, 2007 at 08:49:33AM -0800, krishnamanenia...@gmail.com wrote regarding regular expressions:
hi i am looking for pattern in regular expreesion that replaces
anything starting with and betweeen http:// until /
likehttp://www.start.com/startservice/yellow/fdhttp://helo/abcdwill
be replaced as
p/startservice/yellow/ fdp/abcd

You don't need regular expressions to do that. Look into the methods that strings have. Look at slicing. Look at len. Keep your code readable for future generations.

Py>>help(str)
Py>>dir(str)
Py>>help(str.startswith)

Cheers,
Cliff
Look again at the sample input. Some of the OP's replacement targets
are not at the beginning of a word, so str.startswith wont be much
help.

Here are 2 solutions, one using re, one using pyparsing.

-- Paul
instr = """
anything starting with and betweeen "http://" until "/"
like http://www.start.com/startservice/yellow/ fdhttp://helo/abcd
will
be replaced as
"""

REPLACE_STRING = "p"

# an re solution
import re
print re.sub("http://[^/]*", REPLACE_STRING, instr)
# a pyparsing solution - with handling of target strings inside quotes
from pyparsing import SkipTo, replaceWith, quotedString

replPattern = "http://" + SkipTo("/")
replPattern.setParseAction( replaceWith(REPLACE_STRING) )
replPattern.ignore(quotedString)

print replPattern.transformString(instr)
Prints:

anything starting with and betweeen "p/"
like p/startservice/yellow/ fdp/abcd will
be replaced as
anything starting with and betweeen "http://" until "/"
like p/startservice/yellow/ fdp/abcd will
be replaced as

Nov 6 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.