简体   繁体   English

从一系列字符串中删除不需要的子字符串

[英]Removing the unwanted substrings from a series of strings

I have a series of strings as given below 我有如下所示的一系列字符串

Tata Jaguor 1474 pSNL Series Car
Tata Nano Pro 5864 Series Car
Tata Indica 8586 k5478
Tata Nano 5864 E5478
Tata Bolero 8974 1567 Series

After stripping the unwanted string the resultant string expecting is given respectively as follows 剥离不需要的字符串后,期望的结果字符串分别如下所示

  Jaguor 1474
  Nano 5864 
  Indica 8586_k5478
  Nano 5864_E5478
  Bolero 8974_1567

i tried the following code to do this,but not got the expected result 我尝试下面的代码来做到这一点,但没有得到预期的结果

Vehiclename.replace("Tata ","").replace("Series","").replace("Pro ","").replace(" Car","")

Is there any other better way to do this? 还有其他更好的方法吗?

reobj = re.compile(r"Tata ([\w ]+\d+).*?$", re.IGNORECASE | re.MULTILINE)
result = reobj.sub(r"\1", subject)

http://rubular.com/r/jvvtCjlTKy http://rubular.com/r/jvvtCjlTKy

Jaguor 1474
Nano Pro 5864
Indica 8586 k5478
Nano 5864 E5478
Bolero 8974 1567

In conjunction with my comment: 结合我的评论:

In your example, you have Tata but you are trying to replace Tata<space><space> . 在您的示例中,您拥有Tata但是您试图替换Tata<space><space> series in your example is written differently and the same goes for Pro , digging the documentation for the String replace() 示例中的series使用不同的方式编写,而Pro编写方式也是如此,这将挖掘String replace()的文档

The method replace() returns a copy of the string in which the occurrences of old have been replaced with new, optionally restricting the number of replacements to max. 方法replace()返回字符串的副本,在该字符串中,已出现的旧内容已被新内容替换,可以选择将替换次数限制为最大。

In your case, you do not seem to be doing anything with the returned value, this should work: 在您的情况下,您似乎没有对返回值做任何事情,这应该可以工作:

Vehiclename = Vehiclename.replace("Tata ","").replace("Series","").replace("Pro ","").replace(" Car","")

Following is the regular expression for you: 以下是适合您的正则表达式:

'\s.*[0-9]{4}'

You can implement in python. 您可以在python中实现。 This isnt removing what you are 'not' looking for instead giving what you are looking for. 这并不是删除您“不”要查找的内容,而是给出您要查找的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM