-4

I have to replace any expression which look like:

"any_expression/2016.2

Can I do it with Regex?

String any_expressin can contain any character and in any length

Jirka Picek
  • 589
  • 5
  • 19
sarad
  • 117
  • 1
  • 1
  • 8

2 Answers2

0

Of course you can, this should do it:

".*\/2016\.2

In Python:

str.replace(".*\/2016\.2", "whatYouWantInstead");
0

Answer to your question is "Yes".

If you want to know more about it then:

Regular expression matching your requirements is:

^".*(\/2016\.2)$

If it can be anywhere in text, just remove first and last character:

".*(\/2016\.2)

According to this question Python doesn't recognize regex for str.replace, then you need to use re.sub. It should look like:

import re
line = re.sub(r"^\".*(\/2016\.2)$", r"replaced any expression\1", line)
Community
  • 1
  • 1
Jirka Picek
  • 589
  • 5
  • 19
  • How the following should look wirh re? `exp_cont = list(map(lambda x: x.replace("\".*\/2016\.2", "\"$vivado_path/2016.2/"), exp_cont))` – sarad Mar 15 '17 at 13:45
  • I tried: `exp_cont = list(map(lambda x: re.sub(r"^\".*(\/2016\.2)", r"\"$vivado_path/2016.2", x), exp_cont))` For some reason it write the \ char in the begining of each line which was replaced. – sarad Mar 15 '17 at 14:07