|
Posted by Bosconian on 10/11/18 11:53
"Rik" <luiheidsgoeroe@hotmail.com> wrote in message
news:6dd9a$44befb27$8259c69c$26577@news1.tudelft.nl...
> Bosconian wrote:
>> "Rik" wrote:
>>> Bosconian wrote:
>>>> "Bosconian" wrote:
>>>>> I need a regular expression to validate characters for a given file
>>>>> name. I want to limit valid characters to the following:
>>>>>
>>>>> a-z
>>>>> A-Z
>>>>> 0-9
>>>>> underscores
>>>>> hyphens
>>>>> white spaces
>>>>>
>>>>> The file name must end with a period and a 3-character extension.
>>>>>
>>>>> I also want to prevent repeat underscores, hyphens and white
>>>>> spaces. Example: "__", "--" and " " would all be invalid.
>>>>>
>>>>> I found this example:
>>>>>
>>>>> ^[a-zA-Z0-9_\s-]+$
>>>>>
>>>>> This comes close, but it allows repeat underscores, hyphens and
>>>>> white spaces. It also uses "\s" which allows carriage returns,
>>>>> linefeeds, tabs, etc.
>>>
>>> I thought you wanted to allow whitespaces?
>>>
>>>>> Any help would be greatly appreciated.
>>>> I almost have it:
>>>>
>>>> $pattern = "|[\wa-zA-Z0-9\-_]+\.[a-zA-Z0-9]{3}$|";
>>> If you eant a WHOLE string to match, enchor it with both ^ and $
>>>
>>> /^([a-z0-9]+[\040_\-]?)*\.[a-z]{3}$/i
>>>
>>> Grtz,
>> What about preventing a space before the period? For example:
>> filename .txt
>
> He didn't specify that, so I didn't bother. (allthough a consequence from
> my
> regex is the name should start with somethning from [a-zA-z0-9], which he
> didn't specify either).
>
> 80% of the work with regular expressions is figuring out what the exact
> conditions are. If the regular expressions does what it's supposed to do,
> but doesn'r behave like you'd expect, it clear you've got your
> specifications wrong.
>
> Offcourse, one could:
>
> (1)
> /^([\040_\-]?([a-z0-9]+)*\.[a-z]{3}$/i
> (end with a character in [a-zA-Z0-9] before the dot.)
>
> (2)
> /^[a-z0-9]+([\040_\-]?([a-z0-9]+)*\.[a-z]{3}$/i
> (begin & end with a character in [a-zA-Z0-9] before the dot.)
>
> If we're _assuming_ it's a file_type validation, the '\.[a-z]{3}' isn't
> valid either, lot's of filetypes have an extention shorter or longer then
> 3
> characters, and not all int the range of {a-z]. (For instance mp3 or info)
>
> Grtz,
> --
> Rik Wasmus
>
>
It seems the open parenthesis after the question mark was hindering the
subsequent expression. Removing it will achieve the desired result (i.e.
allowing only a-z 0-9 characters before the period.)
/^[a-z0-9]+([\040_\-]?[a-z0-9]+)*\.[a-z]{3}$/i
[Back to original message]
|