Home > other >  How to use regular expression to get file name only
How to use regular expression to get file name only

Time:10-20

aa/bb/cc/20221020/ac8d1af8c1e34cbd935c5695a073f1c7.mp4
aa/bb/cc/20221020/45b4ee6025fa4e4a99e3b3b7353dde22.mp4
aa/bb/cc/20221020/7f08f76e1c244b61bbb9065fc8b6ff1f.mp4
aa/bb/cc/20221020/ac8d1af8c1e34cbd935c5695a073f1c7.mp4
aa/bb/cc/20221020/ee1bcf0f202741c7a0e25fce37273b74.mp4
aa/bb/cc/20221020/8206afe9bbd04ac6b371c6cc16ba7369.mp4
aa/bb/cc/20221020/027d7780c9084318b743ebc39284e375.mp4
aa/bb/cc/20221020/4db2c18073ba4d54a6343317f3d36ff5.mp4
aa/bb/cc/20221020/ba8f22a0b8af47f1b74b8b3dcff80827.mp4

I want to extract only the file name out of the entire path.

ac8d1af8c1e34cbd935c5695a073f1c7
45b4ee6025fa4e4a99e3b3b7353dde22
7f08f76e1c244b61bbb9065fc8b6ff1f
ac8d1af8c1e34cbd935c5695a073f1c7
ee1bcf0f202741c7a0e25fce37273b74
8206afe9bbd04ac6b371c6cc16ba7369
027d7780c9084318b743ebc39284e375
4db2c18073ba4d54a6343317f3d36ff5
ba8f22a0b8af47f1b74b8b3dcff80827

How can I have only the filename with a regex?

Please give me a brief explanation.

CodePudding user response:

use regex: (?<=\/)\w (?=(.mp4)) or more dyanamic /(?<=\/)\w (?=(\.\w ))/gim

Explanation: find data between slash(/) and (.mp4)

Demo: https://regex101.com/r/FoIpuE/1

To understand regex in detail follow the link: medium.com

CodePudding user response:

\w{2}\/\w{2}\/\w{2}\/\d{8}\/(\w )\.\w 

The group(1) should give you the file name. Try it at https://regex101.com/.

  • Related