Checking for a valid filename using Reg expressions - HELP!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • papillonimages@gmail.com

    #1

    Checking for a valid filename using Reg expressions - HELP!

    Please can someone help me, I am trying to write a Regular Expression
    check for a valid (Windows) filename (cant believe I couldnt find one
    on the net already?!?!?!). Anyway, here is what I have but it doesnt
    fully work - e.g. it allows the use of quotes, and the ampersand in
    some positions and I just cant correct it??! Sorry for being a dull
    newbie.

    TIA.

    -------------------------------------------------

    RegExp = /^["']?(.)?([^\|\&\<\>\;\*\? \\\\\/:]+\.\w+)["']?$/

  • Mick White

    #2
    Re: Checking for a valid filename using Reg expressions - HELP!

    papillonimages@ gmail.com wrote:
    [color=blue]
    > Please can someone help me, I am trying to write a Regular Expression
    > check for a valid (Windows) filename (cant believe I couldnt find one
    > on the net already?!?!?!). Anyway, here is what I have but it doesnt
    > fully work - e.g. it allows the use of quotes, and the ampersand in
    > some positions and I just cant correct it??! Sorry for being a dull
    > newbie.
    >
    > TIA.
    >
    > -------------------------------------------------
    >
    > RegExp = /^["']?(.)?([^\|\&\<\>\;\*\? \\\\\/:]+\.\w+)["']?$/
    >[/color]

    File name or file path?
    Mick

    Comment

    • papillonimages@gmail.com

      #3
      Re: Checking for a valid filename using Reg expressions - HELP!

      file name - path not required...

      Comment

      • Mick White

        #4
        Re: Checking for a valid filename using Reg expressions - HELP!

        papillonimages@ gmail.com wrote:
        [color=blue]
        > file name - path not required...
        >[/color]
        What are the rules for a Windows file name?
        Mick

        Comment

        • Dr John Stockton

          #5
          Re: Checking for a valid filename using Reg expressions - HELP!

          JRS: In article <1109270689.322 623.109880@f14g 2000cwb.googleg roups.com>
          , dated Thu, 24 Feb 2005 11:01:45, seen in news:comp.lang. javascript,
          papillonimages@ gmail.com posted :[color=blue]
          >Please can someone help me, I am trying to write a Regular Expression
          >check for a valid (Windows) filename (cant believe I couldnt find one
          >on the net already?!?!?!). Anyway, here is what I have but it doesnt
          >fully work - e.g. it allows the use of quotes, and the ampersand in
          >some positions and I just cant correct it??! Sorry for being a dull
          >newbie.[/color]
          [color=blue]
          >RegExp = /^["']?(.)?([^\|\&\<\>\;\*\? \\\\\/:]+\.\w+)["']?$/[/color]

          For a start, the close quote must match the open quote. But only " can
          be used for that.

          RegExp = /^("?)(.)?([^\|\&\<\>\;\*\? \\\\\/:]+\.\w+)\1$/

          I don't see why you allow a possible arbitrary character after the first
          optional quote.

          You appear to disallow the colon, meaning that you cannot specify a
          drive, and you cannot specify a path. ISTM that in general the drive
          and path should be allowed; include ([a-zA-Z]:)? and allow multiples
          of what you have above with \ in between - which may be impossible.

          You seem to require an extension; I'm not sure of that. In the
          extension, you allow only word characters; an extension can, ISTM,
          contain all the characters that the rest can - I frequently use $$$, for
          example, and sometimes £££.

          In your list of barred characters, I'd put colon before slash.
          Javascript won't mind, but it will be a little easier to read.


          If these are meant to be names of existing files, why bother? If the
          file can be found, the name is OK; otherwise, it is wrong. If they are
          meant to be names of new files, do you need to allow all possibilities?
          you might list instead those that are needed, which has the benefit of
          rejecting those of value above 126.


          You should have given a written description of the intended criteria;
          then it would have been easier to check your intent and your
          implementation thereof.

          --
          © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
          <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
          <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
          <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

          Comment

          Working...