D
D
Dmitri2020-08-02 11:28:32
Python
Dmitri, 2020-08-02 11:28:32

How to get a value using regular expressions?

'#9f8757'
'#d59005']
'#edf8f0'
'#77d525'
'#C36728'
['#e1331b'
'#b97601'
'#aa9169'
'#55672b'
'#2684a6'
'#e5d5bc'


It is necessary to pull out the value between ' ' from here

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry, 2020-08-02
@dmitriy337

If this data is in some file, then:

from pathlib import Path
import re

filename = 'test123.txt'

content = Path(filename).read_text()
result = re.findall(r"'(#[0-9a-fA-F]+)'", content)

print (result)

P
PavelMos, 2020-08-02
@PavelMos

(\'.*\')
reflecting characters before quotes, dot - any character, * - take the following characters indefinitely, ( ) - each found combination will be saved as a separate result.
If the string format is known - it starts with #, then 6 characters of letters and numbers, then IMHO it's easier and clearer to look for this combination
(#[a-zA-Z0-9]{6})
# first character
[a-zA-Z0- 9] character ranges - lowercase, uppercase Latin + numbers
{6} - take where exactly 6 characters
( ) - each combination found will be a separate result
For python, you can try regexps here:
https://pythex.org/

V
Vadim Shatalov, 2020-08-02
@netpastor

https://regex101.com/#python is more like an online tester

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question