Home > other >  remove substring with space
remove substring with space

Time:07-11

I want to convert bellow string

JS00001.mat 16 24 1000/mV 16 0 -5 28232 0 aVR

to this string in python

-5 28232 0 aVR

I can do that with this code

regex = r'JS[0-9][0-9][0-9][0-9][0-9].mat 16 24 1000/mV 16 0 '
s = re.sub(regex,"",'JS00001.mat 16 24 1000/mV 16 0 517 -22376 0 III')

but it can't removes this part

JS00001.mat 16 24 1000/mV 16 0

please help me.

CodePudding user response:

As far as I can tell, you want the last 4 values of the string. You can do this without regex, like so:

string = r"JS00001.mat 16 24 1000/mV 16 0 -5 28232 0 aVR"
last_four_values = string.split(maxsplit=5)[-1] 
last_four_values = " ".join(string.split()[-4:]) # Or another way
print(last_four_values)

Output:

-5 28232 0 aVR

What other strings do you need to convert?

See:

CodePudding user response:

import re

regex = r".*(-5 28232 0 aVR)"

test_str = "JS00001.mat 16 24 1000/mV 16 0 -5 28232 0 aVR"

subst = "\\1"
result = re.sub(regex, subst, test_str)

if result:
    print (result)


 //Ouput
  
 -5 28232 0 aVR
  • Related