简体   繁体   中英

Converting Script from Powershell to Python-Regex Not Working as Expected

I'm trying to convert a Powershell Script to a python script. I was going to use a Shell script for the ease of use for grep and curl, but I decided on python for the ease of if statements. This is the Powershell code that I am trying to convert:

Powershell code (works great):

$ReturnedRegExData = SearchStringAll -StringToSearch $Data -RegEx $ImgURLRegex 

if ($ReturnedRegExData) #Check Existance of Matches
{
    foreach ($Image in $ReturnedRegExImageData) #Run Through all Matches
    #Can then get the result from the group of results and run through them 1 at a time via $Image
}
else
{
    #exit
}

This is my attempt at Python, not working too good

ReturnedRegExData = re.findall($ImgURLRegex , $Data)

if ReturnedRegExImageData: #Check existance of Matches (Works)
    print "found"
else:
    sys.stderr.write("Error finding Regex \r\n")
    return

$For Loop running through results

re.search worked with this print ReturnedRegExImageData.group(0), but I want to find all matches, and having an extremely hard time replicating foreach ($Image in $ReturnedRegExImageData) this line: I've tried messing around with for Image in ReturnedRegExData and a for loop from 0 to len(ReturnedRegExData), but they don't return valid data. I know Python is supposed to be simple coding, but I am having an extremely hard time of dealing with it.

I've read similar posts for .match, /search and .findall, and they all go over the searching part, but nothing goes over how to get the results in a useful format. I have looked through the manual, but I'm having a hard time deciphering that as well.

How can I run through the results that findall found, whether returns 0, 1 or more results. 0 Should be covered by the if statement.

Thanks for any help you can provide.

J

The findall function returns a list of strings. So you can do something like:

found = re.findall(img_url_regex, data)
if not found: # the list is empty
    sys.stderr.write("Error finding Regex \r\n")
else:
    for imgurl in found:
        print 'Found image:', imgurl
        # whatever else you want to do with the URL.

Note that using $ to start variable names is not valid python;

In [3]: $foo = 12
  File "<ipython-input-3-38be62380e9f>", line 1
    $foo = 12
    ^
SyntaxError: invalid syntax

If you want to replace parts of the found URLs, you can use sub() method. It uses the MatchObject . Below is an example from one of my own scripts. I use it to change eg <img alt='pic' class="align-left" src="static/test.jpg" /> to <img alt='pic' class="align-left" src="static/images/test.jpg" />

with open(filename, 'r') as f:
    data = f.read()
# fix image links
img = re.compile(r'src="[\./]*static/([^"]*)"')
data = img.sub(lambda m: (r'src="' + prefix + 'static/images/' + 
                          m.group(1) + r'"'), data)
with open(filename, 'w+') as of:
    of.write(data)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM