-1

I have a huge file and need to replace some strings, the problem is that they are dynamic but always follow a pattern:

year[4 digits number]/month[2 digits number]/timestamp[8 digits number]/file[random string ending with extension]

Some examples:

2017/07/24204301/a-4.png
2017/07/24204318/a-5-e1501986401369.png
2017/11/24211223/questao10branca-172x300.png

I need to remove the timestamp on all occurrences, then the above example would become:

2017/07/a-4.png
2017/07/a-5-e1501986401369.png
2017/11/questao10branca-172x300.png

How can I achieve this using Regexp and Visual Studio Code?

1 Answers1

0

Given the examples you presented, there are a couple of regular expressions that will work for you.

See regex in use here

/\d{8}(?=/)
  • / Match this literally
  • \d{8} Match any digit exactly 8 times
  • (?=/) Positive lookahead ensuring what follows is a literal /

See regex in use here

(?<=^\d{4}/\d{2}/)\d{8}/
  • (?<=^\d{4}/\d{2}/) Negative lookbehind ensuring what precedes is the following:
    • ^ Assert position at the start of the string
    • \d{4} Match any digit exactly 4 times
    • / Match this literally
    • \d{2} Match any digit exactly twice
    • / Match this literally
  • \d{8} Match any digit exactly 8 times
  • / Match this literally
ctwheels
  • 19,377
  • 6
  • 29
  • 60