python regex to flip a string

foreword

Regulars are really complicated, especially for those of us who don’t use regulars much but also use them. This method of flipping a string can only be used in python, and it is estimated that it will not work in other languages.

code demo

Simple test code:

url = "https://www.baidu.com/Page=2342132&Estrac=238uju-kfhsdjhbg-098-fdjskah2"

If there is such a url, after the crawler crawls the data, I need to put it into the database, I want to get the string of Estrac, how can I match it? If you use

re.search('[\w-]+', url)

In this way, only https can be obtained. Of course, I want to reverse it. What should I do? Does the for loop reverse the elements one by one? Too laborious:

url[::-1]

In python, all its strings can be reversed just like this, and the print output gets the result:

2haksjdf-890-gbhjdshfk-uju832=cartsE&2312432=egaP/moc.udiab.www//:sptth

Then I match it with the regex just now. After matching, I flip the result obtained by the regex [::-1] again, and I can get the result I want. The code is small and easy to understand:

import re

url = "https://www.baidu.com/Page=2342132&Estrac=238uju-kfhsdjhbg-098-fdjskah2"

matchobj = re.match('[\w-]+', url[::-1])
if matchobj:
    result = matchobj.group(0)
print(result[::-1])

The resulting value is:

238uju-kfhsdjhbg-098-fdjskah2

Haven't come across this before, so record it

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324433048&siteId=291194637