Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Compatibility of Python Regex

I am just starting to use regular expressions and have done some tutorials. I order to design and test a regular expression, I use the following website: REGEXR. When typing my regex into that website, it seems to find all substrings that I am interested in. However, when I use the same pattern in Python, it doesn´t seem to work.

The search pattern is given by:

\n(0.00(75|25)00 -?0.00\d\d00 -0.006420 [-\d. ]+)

The string that I am using that pattern on is given by (regexr shows 8 matched):

-0.007500 0.007500
-0.007500 0.007500
-0.006420 0.008580
0.007500 0.007500 -0.006420 -0.000009 0.000036 -0.000052
0.007500 0.002500 -0.006420 -0.000690 0.000143 0.000388
0.007500 -0.002500 -0.006420 -0.000656 -0.000652 0.000334
0.007500 -0.007500 -0.006420 -0.000888 -0.001786 -0.000120
0.002500 0.007500 -0.006420 -0.000016 0.000025 -0.000334
0.002500 0.002500 -0.006420 -0.000135 0.000073 -0.000283
0.002500 -0.002500 -0.006420 -0.000136 -0.000318 -0.000223
0.002500 -0.007500 -0.006420 -0.000069 -0.000958 -0.000240
-0.002500 0.007500 -0.006420 -0.001001 0.000424 -0.000237
-0.002500 0.002500 -0.006420 -0.001230 0.000172 -0.000203
-0.002500 -0.002500 -0.006420 -0.001184 -0.000153 -0.000169
-0.002500 -0.007500 -0.006420 -0.000977 -0.000418 -0.000237
-0.007500 0.007500 -0.006420 -0.000694 0.000236 -0.000289
-0.007500 0.002500 -0.006420 -0.000800 0.000096 -0.000239
-0.007500 -0.002500 -0.006420 -0.000779 -0.000062 -0.000226
-0.007500 -0.007500 -0.006420 -0.000665 -0.000206 -0.000233
0.007500 0.007500 -0.001420 -0.002032 0.000995 0.000694
0.007500 0.002500 -0.001420 -0.002424 0.000385 0.001021
0.007500 -0.002500 -0.001420 -0.002403 -0.000365 0.001048
0.007500 -0.007500 -0.001420 -0.002036 -0.001007 0.000724
0.002500 0.007500 -0.001420 -0.001346 0.000568 0.000189
0.002500 0.002500 -0.001420 -0.001549 0.000213 0.000374
0.002500 -0.002500 -0.001420 -0.001574 -0.000215 0.000348
0.002500 -0.007500 -0.001420 -0.001332 -0.000563 0.000197
-0.002500 0.007500 -0.001420 -0.000905 0.000337 -0.000003
-0.002500 0.002500 -0.001420 -0.001017 0.000114 0.000048
-0.002500 -0.002500 -0.001420 -0.001030 -0.000119 0.000051
-0.002500 -0.007500 -0.001420 -0.000893 -0.000309 -0.000010
-0.007500 0.007500 -0.001420 -0.000632 0.000179 -0.000113
-0.007500 0.002500 -0.001420 -0.000742 0.000086 -0.000080
-0.007500 -0.002500 -0.001420 -0.000709 -0.000049 -0.000077
-0.007500 -0.007500 -0.001420 -0.000621 -0.000167 -0.000096
0.007500 0.007500 0.003580 -0.000749 0.000227 0.000046
0.007500 0.002500 0.003580 -0.000129 0.000020 0.000156
0.007500 -0.002500 0.003580 -0.000168 -0.000323 0.000157
0.007500 -0.007500 0.003580 -0.000053 -0.000643 0.000031
0.002500 0.007500 0.003580 -0.000225 0.000060 0.000032
0.002500 0.002500 0.003580 -0.000509 0.000037 0.000021
0.002500 -0.002500 0.003580 -0.000505 -0.000161 0.000008
0.002500 -0.007500 0.003580 -0.000335 -0.000479 0.000105
-0.002500 0.007500 0.003580 -0.000756 0.000246 0.000188
-0.002500 0.002500 0.003580 -0.000825 0.000085 0.000234
-0.002500 -0.002500 0.003580 -0.000824 -0.000097 0.000230
-0.002500 -0.007500 0.003580 -0.000733 -0.000239 0.000154
-0.007500 0.007500 0.003580 -0.000540 0.000145 0.000019
-0.007500 0.002500 0.003580 -0.000627 0.000058 0.000050
-0.007500 -0.002500 0.003580 -0.000591 -0.000050 0.000056
-0.007500 -0.007500 0.003580 -0.000521 -0.000141 0.000004
0.007500 0.007500 0.008580 -0.000840 0.000375 0.000810
0.007500 0.002500 0.008580 -0.000938 0.000150 0.000993
0.007500 -0.002500 0.008580 -0.000953 -0.000134 0.000989
0.007500 -0.007500 0.008580 -0.000868 -0.000383 0.000833
0.002500 0.007500 0.008580 -0.000727 0.000271 0.000479
0.002500 0.002500 0.008580 -0.000796 0.000109 0.000569
0.002500 -0.002500 0.008580 -0.000844 -0.000087 0.000571
0.002500 -0.007500 0.008580 -0.000747 -0.000261 0.000491
-0.002500 0.007500 0.008580 -0.000577 0.000179 0.000232
-0.002500 0.002500 0.008580 -0.000661 0.000064 0.000293
-0.002500 -0.002500 0.008580 -0.000610 -0.000072 0.000295
-0.002500 -0.007500 0.008580 -0.000584 -0.000168 0.000234
-0.007500 0.007500 0.008580 -0.000447 0.000110 0.000111
-0.007500 0.002500 0.008580 -0.000482 0.000033 0.000113
-0.007500 -0.002500 0.008580 -0.000486 -0.000035 0.000128
-0.007500 -0.007500 0.008580 -0.000449 -0.000107 0.000110

When I do it in python, I use the following code (which returns 0 matches):

#open the file containing the text to be searched
fileptr=open(text.txt)
test=fileptr.read()
pattern=r"\n(0.00(75|25)00 -?0.00\d\d00 -0.006420 [-\d. ]+)"
result=re.findall(pattern,text)

EDIT: Actual Code used:

for i in range(0,8):
    cubefile=[file for file in metrofiles if re.search("mfg-100_00_meas_vfield_0%d"%(i),file)!=None][0]
    metrofile=[file for file in cubefiles if re.search(r"mfg-100_coil_0%d"%(i),file)!=None][0]
    metrofptr=open(metrofile,"r")
    cubefptr=open(cubefile,"r")
    metrofile=metrofptr.read()
    cubefile=cubefptr.read()

    cube_incorrectlines=re.findall(r"\n(0.00(75|25)00 -?0.00\d\d00 -0.006420 [-\d. ]+)",cubefile)

    metrofptr.close()
    cubefptr.close()

Comments