dcs
dcs

Reputation: 11

substring extraction with specific format

I have a string which contains a substring with the next format: it starts with i_ or o_ prefix and ends with , comma.

How can I extract such string?

For example:

string = 'input  [89:0]     i_gth_rxdata,'

I need i_gth_rxdata

Upvotes: 1

Views: 85

Answers (3)

unutbu
unutbu

Reputation: 879691

In [6]: import re

In [7]: text = 'input [89:0] i_gth_rxdata,'

In [9]: re.search(r'([io]_.*),', text).group(1)
Out[9]: 'i_gth_rxdata'

.* will match greedily until the last comma found. If you want the pattern to match until the first comma found, use r'([io]_.*?),'.

Here is an example which shows the difference:

In [15]: re.search(r'([io]_.*),', 'input [89:0] i_gth_rxdata, 1,').group(1)
Out[15]: 'i_gth_rxdata, 1'

In [16]: re.search(r'([io]_.*?),', 'input [89:0] i_gth_rxdata, 1,').group(1)
Out[16]: 'i_gth_rxdata'

Upvotes: 0

laike9m
laike9m

Reputation: 19348

>>> s = 'input [89:0] i_gth_rxdata,'
>>> import re
>>> obj = re.search(r'(i_|o_).*,', s)
>>> obj.group()
'i_gth_rxdata,'

Upvotes: 0

thefourtheye
thefourtheye

Reputation: 239483

my_string = 'input  [89:0]     i_gth_rxdata,'
import re
print re.findall(r'[io]_.*?(?=,)', my_string)
# ['i_gth_rxdata']

You can see how that RegEx works, in this online demo

[io]_.*?(?=,)

Regular expression visualization

Debuggex Demo

Upvotes: 4

Related Questions