Home > Software design >  What can be the regular expression or regex for a file path or path?
What can be the regular expression or regex for a file path or path?

Time:01-06

I am new to this.Please can anyone tell me what can be the regular expression for file path or any path :

Ex => "C:\\Users\\1700000\\Downloads\\BackendApp\\WebApplication\\WebAPI_APPL\\Data\\1\\FirstFol\\SecondFodler\\MainFolder\\File.xlsx" or "C:\\Users\\1700000\\Downloads\\BackendApp\\WebApplication\\WebAPI_APPL\\Data\\1\\FirstFol\\SecondFodler\\MainFolder"

File path or path can be small or big, it is not fixed. Path should start with "C(any drive):\\" Please let me know what can I use also the expression should consider double backslash in it.

This expressions didn't work

  1. @"^(?:[\w]:|\\)(\\[a-z_-\s0-9.] ) .(txt|gif|pdf|doc|docx|xls|xlsx)$"
  2. @"^(?:[a-zA-Z]:|\\\\[\w.] \\[\w.$] )\\(?:[\w] \\)*\w([\w.]) $"

CodePudding user response:

try with this one: ^(?:[\w]:|\)([A-Za-z_\s0-9.-\] ).(txt|gif|pdf|docx|doc|xlsx|xls)$

you could use https://regex101.com/ to try yours regex

CodePudding user response:

Here's one i've used in the past for unix based systems

(\/)((\w|-|_|[0-9])*\/(\w|-|_|[0-9])*) [^.*\.]

For windows it will be essentially the same detecting drive letter :

([A-Z]{1}:)(\/)((\w|-|_|[0-9])*\/(\w|-|_|[0-9])*) [^.*\.]*

With backslashes :

([A-Z]{1}:)(\\\\)((\w|-|_|[0-9])*\\\\(\w|-|_|[0-9])*) [^.*\.]*

When I used it I expected my strings to be paths only. It may capture text after a path so it's not perfect. But I hope this helps.

  • Related