Possible Duplicate:
C# - Regex for file paths e.g. C:\test\test.exe
I was trying out to create a regular expression to match file path in java
Like C:/WINDOWS/Profiles/myComputer/Desktop/test.xml
Pls help me.
Thank you very much
Possible Duplicate:
C# - Regex for file paths e.g. C:\test\test.exe
I was trying out to create a regular expression to match file path in java
Like C:/WINDOWS/Profiles/myComputer/Desktop/test.xml
Pls help me.
Thank you very much
you could try this,
(?:[\w]\:|\\)(\\[a-z_\-\s0-9\.]+)+\.(?i)(txt|xml|gif|pdf|doc|docx|xls|xlsx)$
Explanation:
^(?:[\w]\:|\\) -- Begin with x:\ or \\
[a-z_\-\s0-9\.] -- valid characters are a-z| 0-9|-|.|_ (you can add more)
(?i) -- regular expression case-insensitive
(txt|xml|gif|pdf|doc|docx|xls|xlsx) -- Valid extension (you can add more)
Matcher ma = Pattern.compile("([a-zA-Z]:(?:/[\\w\\s]+)*/[\\w\\s]+\\.\\w+)")
.matcher("C:/WINDOWS/Profiles/myComputer/Desktop/test.xml");
while (ma.find()) {
System.out.println(ma.group(1));
}
Here's an example working for your case. maybe you need to add some characters that aren't allowed but it's just adding chars in [\w\s.] <- that would accept dots too.