Good day. Tell me, can I iterate over the elements of the string? For example, there is a string a = 'qqqq 12qw3 12 12345', from here you need to pull out the element, which will be a number and the length of which will be more than 4 characters. It may be worth using the re module, but I cannot figure out how to set the condition for selecting an element along its length.
1 answer
Line:
In [56]: a = 'qqqq a987654xx321aa 12qw3 12 12345 zz 1234567' If you need to return only the first number whose length is more than 4 characters:
In [72]: re.search(r'\b(\d{5,})\b', a).group(0) Out[72]: '12345' Option not using RegEx :
In [68]: [s for s in a.split() if s.isdecimal() and len(s) > 4][0] Out[68]: '12345' If you want to return all the numbers whose length is more than 4 characters:
In [57]: re.findall(r'\b(\d{5,})\b', a) Out[57]: ['12345', '1234567'] or so, if you count the number 987654 present in a987654xx321aa :
In [58]: re.findall(r'(\d{5,})', a) Out[58]: ['987654', '12345', '1234567'] - And if it is necessary to return only the first element met by condition? - moffire
- @moffire, use
re.search(). - Visman - @jfs, thanks, fixed. - MaxU
|
\d{4,}. - Qwertiy ♦