0

I have this like:

javascript:ColdFusion.Window.show('theformats');ColdFusion.navigate('exportformats.cfm?id=1900067&expformat=bibtex','theformats');

Let's split this into 2 parts:

1) 'exportformats.cfm?id=1900067&expformat=bibtex' 2) all the rest, left and right of it

What the BEST way in Python to get 1) given that 2) never changes?

So far, I have tried "finding" [ColdFusion.navigate('] in the string and slicing from there until [','] but I would really like to learn how to concoct the very best RegEx for it and do so in Python, please.

0

4 Answers 4

1
>>> import re
>>> sample = "javascript:ColdFusion.Window.show('theformats');ColdFusion.navigate('exportformats.cfm?id=1900067&expformat=bibtex','theformats');"
>>> regex = r"javascript:ColdFusion\.Window\.show\('theformats'\);ColdFusion.navigate\('([^']+)','theformats'\);"
>>> print re.match(regex, sample).group(1)
'exportformats.cfm?id=1900067&expformat=bibtex'
Sign up to request clarification or add additional context in comments.

Comments

1

You don't need a regex. Oftentimes, when faced with paired symbols, you can do something like this:

mystr = "javascript:ColdFusion.Window.show('theformats');ColdFusion.navigate('exportformats.cfm?id=1900067&expformat=bibtex','theformats');"
mystr.split("'")[3] # Returns exportformats.cfm?id=1900067&expformat=bibtex

1 Comment

This is perhaps the best solution, unless of course, 1) has single quotes inside of it.
1

I agree with arxanas's answer but if your 1) might include single quotes or other characters in it:

str = "javascript:ColdFusion.Window.show('theformats');ColdFusion.navigate('exportformats.cfm?id=1900067'&expformat=bibtex','theformats');"
str = str.split("javascript:ColdFusion.Window.show('theformats');ColdFusion.navigate('")[1].split("','theformats');")[0]

http://codepad.org/lAk5d6ZV

Comments

0

I believe you're after:

re.search(r"ColdFusion.navigate\('(.*?)'", string).group(1)

Or for before and after:

m = re.match(r"(.*?)ColdFusion.navigate\('(.*?)'(.*)", string)
# m.group(1) == before, m.group(2) = url, m.group(3) = after

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.