A regular Expression for fnding a relative path

Hi.

I need a regular expression for finding relatives paths e.g. ../img/xxx.gif or ./img/xxx.gif or img/xxx.gif  in general ../path/to/file/xxx.gif



I am using python2.5.

Please provide a clear solution...I am not an expert in regex...

Thanks



dmontgomAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

HonorGodSoftware EngineerCommented:
Something like this perhaps?
>>> import re
>>>
>>> def isRelative( path ) :
...   RE = re.compile( r'\.\.?[\\/]' )
...   return ( RE.search( path ) != None )
...
>>> isRelative( r'../img/xxx.gif' )
True
>>>

Open in new window

0
HonorGodSoftware EngineerCommented:
better...
>>> def isRelative( path ) :
...   RE = re.compile( r'(^|[\\/])\.\.?[\\/]' )
...   return ( RE.search( path ) != None )
...
>>> isRelative( 'a./b' )
False
>>> isRelative( './b' )
True
>>> isRelative( 'a../b' )
False
>>> isRelative( 'img/xxx.gif' )
False
>>> isRelative( '../path/to/file/xxx.gif' )
True
>>>

Open in new window

0
HonorGodSoftware EngineerCommented:
best?

By doing it this way, the relative path regular expression is compiled only once
>>> def isRelative( path, RE = None ) :
...   if not RE :
...     RE = re.compile( r'(^|[\\/])\.\.?[\\/]' )
...   return ( RE.search( path ) != None )
...
>>> isRelative( 'a./b' )
False
>>> isRelative( './b' )
True
>>> isRelative( 'a../b' )
False
>>> isRelative( 'img/xxx.gif' )
False
>>> isRelative( '../path/to/file/xxx.gif' )
True
>>>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

HonorGodSoftware EngineerCommented:
ah.  I was thinking of an object method.

Nevermind.  Go with the 2nd... (i.e., better)
0
dmontgomAuthor Commented:
Hi,

My bad...I was not clear....

I have a html page and there are relative tags in java scripts. I need to search the string and find patters that look like a relative url so I can do a find and replace....  I will be looking for files that have extensions if e.g. css,js,jpg etc....

So...if given this string "adadfadf adafd ../test/test.gif adfadfaf"  how can I do this for any arbitrary pattern?
0
HonorGodSoftware EngineerCommented:
Ah, so you want to locate the relative address substring(s) within a string?
0
HonorGodSoftware EngineerCommented:
Something like this?
>>> def Relative( str ) :
...   RE = re.compile( r'(^|[\\/])\.\.?[\\/]' )
...   result = []
...   for data in str.split() :
...     if RE.search( data ) != None :
...       result.append( data )
...   return result
...
>>> Relative( "adadfadf adafd ../test/test.gif adfadfaf" )
['../test/test.gif']
>>>

Open in new window

0
Todd MummertCommented:

The answer HonorGod gave is quite good.   It doesn't match the img/xxx.gif case you gave, but that's easily fixed.

The problem, however, isn't particularly well defined.   For example, is  "foo.gif" a relative path?   Strictly speaking it is, but that may not be what you have in mind.  

Are you looking for all paths that have at least one / (or \) and don't start at the root (don't begin with a /).   What's your platform (Windows, Linux, ...)?

Again, using HG's script above, where it splits into strings first, you can use this:

r'(^[^\\/].*[\\/].*)'

which basically says: 1) don't start w/ a  / or \, then there must be at least one / or \ somewhere else in the string.
0
dmontgomAuthor Commented:
cool...almost there...here a real example...

because of the path between the ' ' it did not find it.....


tt = """ <body onload="MM_preloadImages('../images_home/home1.gif ','../images_home/started1.gif','../images_home/pricing1.gif','../images_home/success1.gif','../images_home/how1.gif','../images_home/about1.gif','../images_home/faqs1.gif','../images_home/home_step01.gif','../images_home/home_step02.gif','../images_home/home_step03.gif')">
"""
0
Todd MummertCommented:
change the split above to:

for data in str.split(''") :

that's double quote, single quote, double quote... I don't think you have to escape the ' quote... but if you do, its

"\'"

basically, instead of splitting on whitespace, we're splitting on the single quotes
0
ghostdog74Commented:
no need for regular expression


tt = """ <body onload="MM_preloadImages('../images_home/home1.gif ','../images_home/started1.gif','../images_home/pricing1.gif','../images_home/success1.gif','../images_home/how1.gif','../images_home/about1.gif','../images_home/faqs1.gif','../images_home/home_step01.gif','../images_home/home_step02.gif','../images_home/home_step03.gif')">
"""
for item in tt.split(","):
    item = item[ item.index("../"):]
    print item[: item.index("'")]

Open in new window

0
dmontgomAuthor Commented:
what is need is regex...there is a larger scope here...

I use beautiful soup is parse html tags....

I need regex it handle arbitrary complexity....

0
dmontgomAuthor Commented:
PS  jiust imagine a big html file.....,is there a regex way to find all relative links?  e.g. / ./ ../ ../../ etc....

HonorGod's code worked great for all all cases...just not if there is a quote e.g.  '' or a " "..  Also it wont work if the rel path is e.g. images/test.gif.

0
HonorGodSoftware EngineerCommented:
Thanks for the grade & points.

Good luck and have a great day.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.