How to search for symbols using regex

General Tech Bugs & Fixes 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

Learning Python and trying to get the User ID from a HTML page, through the use of Regular Expressions. (LTT is the website, just for practice).

I want to be able to type 'findID username' into cmd and return the 6 digit ID number.

Have spent hours trying different code and looking up references, maybe someone can explain it simple for me. I can configure the searchRegex object to correctly identify 6 digit numbers in the page, but it does not find the correct 6 digit combination that I am looking for. (Grabs another random 6 digits as opposed to the 6 specific User ID digits)

import re, requests, sys, time
if len(sys.argv)>1:
    search=requests.get('https://linustechtips.com/main/search/?&q='+str(sys.argv[1:])+'&type=core_members')
    searchRegex=re.compile(r"^'$\d\d\d\d\d\d^'$")
    ID=searchRegex.search(search.text)
    print(ID)
    time.sleep(10)
else:
    print('Enter a search term...')

I have tried many different ways of getting the code to recognise ' symbol. But when i try like this, returns None. Why can the regex find 6 digits, but can't find 6 digits beginning and ending with '.

This is the HTML page I am testing it on.

view-source:https://linustechtips.com/main/search/?&q=missiontomine&type=core_members

profilepic.png
manpreet 2 years ago

Try Regex: (?<=profile\/)\d{6}

Demo

The html text has the userid as part of the url like:

https://linustechtips.com/main/profile/600895-missiontomine/?do=hovercard

(?<=profile\/) does a positive lookbehind


0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.