Hasan Onur ATAÇ
Hasan Onur ATAÇ

Reputation: 51

RegEx for matching a special alphanumeric pattern

Hello Im new at regex so; I want to get only 8 digit char from a complicated code here;

["AA","BB","CC","DD","EE","FF","GG","AAA","SSS","sDDD","mFFFa"],"gaa":"aaa","timasa});
    **var csrf_token = '551a8513';**

    Unimportanttext = '/justanothernonimportanttext&&h=551a8513';

I tried token\s=\s'[^']* but it resuts : ["token = '551a8513"] I just need 551a8513 this part. Can you help me with regex code. Thanks Low english level so may be I have mistakes. Im sorry

EDIT : We got 2 answers in here and both of them works! 1)

token = re.findall("(?<=csrf_token = ')[^\']+",driver.page_source)
print(link + str(token[0]))

2)

token = re.findall("csrf_token = '(.+)'", driver.page_source)
print(link + str(token[0]))

Thank you both of you! I hope you live well! :)

Upvotes: 1

Views: 69

Answers (2)

Emma
Emma

Reputation: 27723

Our desired output has numbers followed by lowercase letters and numbers. We can capture that using an expression similar to:

([0-9]+[a-z]+[0-9]+)

Demo

Or we can capture it using:

(([a-z]+)?([0-9]+))

Demo

Or we can simply use the ' before and after:

'(.+)'

Demo

We can also add more boundaries. Such as:

csrf_token = '(.+)'

Demo

Based on The fourth bird's advice, it is much safe and better to modify it as:

csrf_token = '(.+?)'

Test

# -*- coding: UTF-8 -*-
import re

string = """

["AA","BB","CC","DD","EE","FF","GG","AAA","SSS","sDDD","mFFFa"],"gaa":"aaa","timasa});
    **var csrf_token = '551a8513';**

    Unimportanttext = '/justanothernonimportanttext&&h=551a8513';

"""
expression = r'csrf_token = \'(.+?)\''
match = re.search(expression, string)
if match:
    print("YAAAY! \"" + match.group(1) + "\" is a match 💚💚💚 ")
else: 
    print('🙀 Sorry! No matches!')

Output

YAAAY! "551a8513" is a match 💚💚💚

Upvotes: 2

dvo
dvo

Reputation: 2153

Try this positive lookbehind pattern:

(?<=csrf_token = ')[^\']+

This will return whatever is wrapped in single quotes ('value') after "csrf_token = ". Basically, it will match what you were getting but only return the part of the string you care about. Hope this helps.

Upvotes: 0

Related Questions