Skip to content
Advertisement

Extract digits from string by condition

I want to extract digits from a short string, base on a condition that the digits is in front of a character (S flag).

example and result:

JavaScript

I can split the string to a list to get the individual element,

JavaScript

but how could I just get the 18 and 10?

Advertisement

Answer

Use re.findall with the regex r'(d+)S'. This matches all digits before a capital S.

JavaScript

To get integer output, you can convert them in a list comp or use map

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement