Home > Blockchain >  RegEx: how to use regex to search within a webpage
RegEx: how to use regex to search within a webpage

Time:03-19

say I have these webpages:

  • 2014_FIFA_World_Cup_en.wikipedia.org_mobile-web_all-agents .xxx_en.wikipedia.org_all-access_all-agents 世間情_zh.wikipedia.org_all-access_all-agents A_Song_of_Ice_and_Fire_en.wikipedia.org_desktop_all-agents 1._Juli_de.wikipedia.org_desktop_all-agents

How can i write the regex code to get only the devices used in the searches? where the devices could be :mobile, desktop or all-access

CodePudding user response:

import re

list = [
    '2014_FIFA_World_Cup_en.wikipedia.org_mobile-web_all-agents',
    '.xxx_en.wikipedia.org_all-access_all-agents',
    '世間情_zh.wikipedia.org_all-access_all-agents',
    'A_Song_of_Ice_and_Fire_en.wikipedia.org_desktop_all-agents',
    '1._Juli_de.wikipedia.org_desktop_all-agents'
]

for item in list:
    m = re.search('\\.wikipedia\\.org_(desktop|mobile-web|mobile-app|all-access)(?:_|$)', item);
    print('Device: {0}'.format(m.group(1) if m else 'None'))

# Prints:
#
# Device: mobile-web
# Device: all-access
# Device: all-access
# Device: desktop
# Device: desktop

CodePudding user response:

You can try something like this: match = re.match("(mobile|all-access|desktop)",your website name here)

  • Related