Reputation: 51
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
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]+)
Or we can capture it using:
(([a-z]+)?([0-9]+))
Or we can simply use the '
before and after:
'(.+)'
We can also add more boundaries. Such as:
csrf_token = '(.+)'
Based on The fourth bird's advice, it is much safe and better to modify it as:
csrf_token = '(.+?)'
# -*- 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!')
YAAAY! "551a8513" is a match 💚💚💚
Upvotes: 2
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