views:

43

answers:

2

Hay this regualr expression working fine for Full Windows Folder Path

^([A-Za-z]:|\\{2}([-\w]+|((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))\\(([^"*/:?|<>\\,;[\]+=.\x00-\x20]|\.[.\x20]*[^"*/:?|<>\\,;[\]+=.\x00-\x20])([^"*/:?|<>\\,;[\]+=\x00-\x1F]*[^"*/:?|<>\\,;[\]+=\x00-\x20])?))\\([^"*/:?|<>\\.\x00-\x20]([^"*/:?|<>\\\x00-\x1F]*[^"*/:?|<>\\.\x00-\x20])?\\)*$

Matches
d:\, \\Dpk\T c\, E:\reference\h101\, \\be\projects$\Wield\Rff\, \\70.60.44.88\T d\SPC2\

Non-Matches
j:ohn\, \\Dpk\, G:\GD, \\cae\.. ..\, \\70.60.44\T d\SPC2\

PROBLEM: THIS EXPRESSION REQUIRED "\" END OF PATH. HOW CAN I EDIT THIS EXPRESSION SO USER CAN ENTER PATH LIKE C:\Folder1, C:\Folder 1\Sub Folder

+1  A: 

I don't understand your regular expression at all. But I bet all you need to do is find the bit or bits that match the trailing "\", and add a single question mark after that bit or those bits.

Borealid
Hay Thnx for your replyI just found this expression from this websitehttp://regexlib.com/REDetails.aspx?regexp_id=1785
SOF User
+2  A: 

There are two ways to approach this problem:

  • Understand the regex (way harder than necessary) and fix it to your specification (may be buggy)
  • Who cares how the regex does its thing (it seems to do what you need) and modify your input to conform to what you think the regex does

The second approach means that you just check if the input string ends with \. If it doesn't then just add it on, then let the regex does it magic.

I normally wouldn't recommend this ignorant alternative, but this may be an exception.


Blackboxing

Here's how I'm "solving" this problem:

  • There's a magic box, who knows how it works but it does 99% of the time
  • We want it to work 100% of the time
  • It's simpler to fix the 1% so it works with the magic box rather than fixing the magic box itself (because this would require understanding of how the magic box works)
  • Then just fix the 1% manually and leave the magic box alone

Deciphering the black magic

That said, we can certainly try to take a look at the regex. Here's the same pattern but reformatted in free-spacing/comments mode, i.e. (?x) in e.g. Java.

^
( [A-Za-z]:
| \\{2}   ( [-\w]+
          | (
               (25[0-5]
               |2[0-4][0-9]
               |[01]?[0-9][0-9]?
               )\.
            ){3}
               (25[0-5]
               |2[0-4][0-9]
               |[01]?[0-9][0-9]?
               )
          )
  \\ (
       (    [^"*/:?|<>\\,;[\]+=.\x00-\x20]
       |  \.[.\x20]* [^"*/:?|<>\\,;[\]+=.\x00-\x20]
       )
       (    [^"*/:?|<>\\,;[\]+=\x00-\x1F]*
            [^"*/:?|<>\\,;[\]+=\x00-\x20]
       )?
     )
)
\\ ( 
         [^"*/:?|<>\\.\x00-\x20]
      (
         [^"*/:?|<>\\\x00-\x1F]*
         [^"*/:?|<>\\.\x00-\x20]
      )?
      \\
   )*
$

The main skeleton of the pattern is as follows:

^
(head)
\\ (
      bodypart
      \\
   )*
$

Based from this higher-level view, it looks like an optional trailing \ can be supported by adding ? on the two \\ following the (head) part:

^
(head)
\\?(
      bodypart
      \\?
   )*
$

References


Note on catastrophic backtracking

You should generally be very wary of nesting repetition modifiers (a ? inside a * in this case), but for this specific pattern it's "okay", because the bodypart doesn't match \.

References

polygenelubricants
@polygenelubricants: But what if adding the "\" makes it work with some paths it shouldn't? I mean, without understanding, who knows whether it will start a global thermonuclear war?
Borealid
@Borealid: Let's say `s` doesn't end with `'\'`. If it's true that the regex requires a terminating `'\'` to match, then it wouldn't have accepted `s` in the first place, so checking if `s + '\'` is accepted will not cause a false positive.
polygenelubricants
Let's say `s` is a string which is *correctly* not accepted currently, and `s+'\'` is an accepted string. If you append '\' to all strings before processing, then `s` is now incorrectly accepted where it previously was not. Thus, a false positive.
Borealid
@polygenelubricantsHay thanks Dear i already did that i added "\" in input string.
SOF User
@Borealid: give an example of such `s`; I'm getting confused.
polygenelubricants
@polygenelubricants: Let `s` be `/`. `//` is a valid path, `/` is not, on Windows.
Borealid
@Borealid: my proposal is to append `'\'` only if `s` doesn't already end with `'\'`. Your `s` ends with a different kind of slash (`'/'`), and you add yet another `'/'`. I'm getting even more confused.
polygenelubricants