标签云

微信群

扫码加入我们

WeChat QR Code

I have a regex related question. If I have a variable name nodeName That I am reading from a .csv file and that can look like any of the following: E1_40873886, E2_40873886, 40873886, 40873886-A, 40873886-B. I can write a long piece of code with if elif ... else but I am wondering if python's regex has a smarter way to do it. Also, I cant hardcode 40873886 like if '40873886' in {entry}: because the .csv file has a million entires and with varying numberIDs.


What do you actually want to do?

2019年04月22日13分37秒

TigerhawkT3 just extract "40873886" portion from above strings. Later I wanna append to an array which I am handling.

2019年04月22日13分37秒

Like re.search('\d\d+', 'E1_45612786188a').group(0)?

2019年04月22日13分37秒

TigerhawkT3 I can't hardcode because there are like a million entries in the .csv file and i am temporarily saving in a variable "tempNode". so can something like re.search('\d\d+', tempNode).group(0) where tempNode has something that looks like one of the following: E1_40873886, E2_40873886, 40873886, 40873886-A, 40873886-B work?

2019年04月22日13分37秒

You'd replace the 'E1_45612786188a' with a reference to the cell you're interested in.

2019年04月22日13分37秒

MoMo, it covers all the possible formats in the question so not sure what you mean

2019年04月22日13分37秒

This works. I have a followup question: what if nodeNames looks like this: nodeNames=[nodeNames = ["40873886_A", "40873886_E1", "40873886_E2", "40873886_RES_NC-SC", "E1_40873886", "E2_40873886", "ELB: (A): 40873886_E1", "ELB: (A): 40873886_E2", "Node_40873886", "txug: p-38: 40873886", "40873886-A", "4087388-B"]

2019年04月22日13分37秒

Slr, is this like, literally how it looks like? Or are you implying that it's an array in an array?

2019年04月22日13分37秒

I mean what if nodeNames can look like any of the above but want to extract 40873886 portion of it?

2019年04月22日13分37秒

Well you could use Padraic's answer above, if u can ensure it always has 8 digits, if you can't assure it, you can modify his regex into "\d{3,}" that is if you can ensure the thing ur extracting has atleast 3 digits.pastebin link

2019年04月22日13分37秒