Dnx_7
asked on
How to extract a path from a given string with REGEX in C#
Hi,
I would like to extract a path from any given string with a regex.
We can have multiple scenario like :
hi my name is G:\Assets\Resources\Firstn ame.png say hello!
G:\Assets\Resources\Firstn ame.png say hello!
Hi my name is G:\Assets\Resources\Firstn ame.png
G:\Assets\Resources\Firstn ame.png
In all cases, the result would be "G:\Assets\Resources\First name.png"
Can you help me to do that?
Thanks in advance,
Kind Regards.
I would like to extract a path from any given string with a regex.
We can have multiple scenario like :
hi my name is G:\Assets\Resources\Firstn
G:\Assets\Resources\Firstn
Hi my name is G:\Assets\Resources\Firstn
G:\Assets\Resources\Firstn
In all cases, the result would be "G:\Assets\Resources\First
Can you help me to do that?
Thanks in advance,
Kind Regards.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
After playing a bit, I came to this one ... worked well in Expresso in a text stream of your examples. Maybe the \r\n part isn't needed for singular one line strings outside of a longer text.
([a-zA-Z]\:\\[\w\\\.]*)[\r \n ]+?.*
BTW: That works only if the path doesn't contain blanks. If the path contains blanks, the task would be an impossible one if there are no definite delimiters.
@Guy Hengel: Your solution seems to omit that the first character in the path must be a letter (not a number or special character). Beside of that it gives some strange results in Expresso ...
([a-zA-Z]\:\\[\w\\\.]*)[\r
BTW: That works only if the path doesn't contain blanks. If the path contains blanks, the task would be an impossible one if there are no definite delimiters.
@Guy Hengel: Your solution seems to omit that the first character in the path must be a letter (not a number or special character). Beside of that it gives some strange results in Expresso ...
If you consider only absolute paths without spaces in it, you can use the following regex:
[a-z]:(\\[^"<>|\u0000-\u001F:*?\\/])*
ASKER
hi,
Thanks all for the help.
frankhelk, it seems that your solution is the closest one but indeed, we can have space in path BUT file extension can be specified like '.png' or '.bmp', can you adapt the regex for me? the regex is definitely not my friend lol
louisfr the regex seems not to be valid...
Thanks!
Thanks all for the help.
frankhelk, it seems that your solution is the closest one but indeed, we can have space in path BUT file extension can be specified like '.png' or '.bmp', can you adapt the regex for me? the regex is definitely not my friend lol
louisfr the regex seems not to be valid...
Thanks!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I'll be back !