Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract characters and numeric values from a given string?

Tags:

python

string

I have the following strings:

'10000 ABC = 1 DEF'
'1 AM = 0,30$'
'3500 ABC = 1 GTY'
'1000 HUYT=1ABC'
'1 MONET Data = 1 ABC'

I want to find a flexible way to extract numeric and string values from left and right sides of =. I do not know all possible string values. Therefore I cannot pre-define them. The only thing that I know is that left and right sides are divided by =.

The goal is to get this result for the above-given example:

String-pairs:

ABC-DEF
AM-$
ABC-GTY
HUYT-ABC
MONET Data-ABC

Numeric-pairs:

10000-1
1-0.30
3500-1
1000-1
1-1

I was trying to use .lstrip('...') and rstrip("..."), but it does not give me the expected result.

like image 716
Markus Avatar asked Feb 25 '26 20:02

Markus


1 Answers

Remove the unwanted characters and replace the = with a -.

import re

str = ['10000 ABC = 1 DEF',
    '1 AM = 0,30$',
    '3500 ABC = 1 GTY',
    '1000 HUYT=1ABC',
    '1 MONET Data = 1 ABC']

String_pairs = []
Numeric_pairs = []

for s in str:
    String_pairs.append (re.sub(r'\s*=\s*','-', re.sub(r'\s*\d+(,\d+)?\s*','', s)))
    Numeric_pairs.append (re.sub(r'\s*=\s*','-', re.sub(r'\s*[^\d,=]+\s*','', s)))

print String_pairs
print Numeric_pairs

Result:

['ABC-DEF', 'AM-$', 'ABC-GTY', 'HUYT-ABC', 'MONET Data-ABC']
['10000-1', '1-0,30', '3500-1', '1000-1', '1-1']

or a more cooler list comprehension (with the same result):

String_pairs = [re.sub(r'\s*=\s*','-', re.sub(r'\s*\d+(,\d+)?\s*','', s)) for s in str]
Numeric_pairs = [re.sub(r'\s*=\s*','-', re.sub(r'\s*[^\d,=]+\s*','', s)) for s in str]
like image 155
Jongware Avatar answered Feb 28 '26 10:02

Jongware