zinoMedlem sedan jan. 200247 inlägg Hej! Jag har följande RegExp för att tillåta och inte tillåta vissa tecken i ett filnamn:
regEx.Pattern = "^(?!^(PRN|AUX|CLOCK\$|NUL|CON|COM\d|LPT\d|\..*)(\..+)?$)(?!.*\s\s)[^\x00-\x1f\\?*:\""""><|/]+$"
Det jag vill göra är att lägga till så att tecknen; & % # + inte heller tillåts, men jag gissar att något av den kod som tillåter tecken "skriver över" dessa.
Jag har försökt att lägga till dom tillsammans med dessa;
?*:\""""><|/
men då ignoreras dessa eller sparas inte i filnamnet. Om jag döper en fil till:
test & test så sparas filen som "test .txt" dvs med ett mellanslag före filendelsen och med & tecknet och det som följer borttaget.
Kan någon se vad som behöver göras för att lösa detta?
:x
Du vill att någon av ?*:\"><|/ inte skall matchas?
Det känns att mönstret inte riktigt stämmer när jag kollar på den.
Är det ett filnamn den ska kolla upp(samma mönster som windows maskiner har?)
Är det endast vad filen slutar på du skall kontrollera?
Jag bifogar vad RegExp buddy skickar till mig som beskriver ditt uttryck:
"^" + // Assert position at the beginning of a line (at beginning of the string or after a line break character)
"(?!" + // Assert that it is impossible to match the regex below starting at this position (negative lookahead)
"^" + // Assert position at the beginning of a line (at beginning of the string or after a line break character)
"(" + // Match the regular expression below and capture its match into backreference number 1
// Match either the regular expression below (attempting the next alternative only if this one fails)
"PRN" + // Match the characters “PRN” literally
"|" + // Or match regular expression number 2 below (attempting the next alternative only if this one fails)
"AUX" + // Match the characters “AUX” literally
"|" + // Or match regular expression number 3 below (attempting the next alternative only if this one fails)
"CLOCK" + // Match the characters “CLOCK” literally
"\\$" + // Match the character “$” literally
"|" + // Or match regular expression number 4 below (attempting the next alternative only if this one fails)
"NUL" + // Match the characters “NUL” literally
"|" + // Or match regular expression number 5 below (attempting the next alternative only if this one fails)
"CON" + // Match the characters “CON” literally
"|" + // Or match regular expression number 6 below (attempting the next alternative only if this one fails)
"COM" + // Match the characters “COM” literally
"\\d" + // Match a single digit 0..9
"|" + // Or match regular expression number 7 below (attempting the next alternative only if this one fails)
"LPT" + // Match the characters “LPT” literally
"\\d" + // Match a single digit 0..9
"|" + // Or match regular expression number 8 below (the entire group fails if this one fails to match)
"\\." + // Match the character “.” literally
"." + // Match any single character that is not a line break character
"*" + // Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
")" +
"(" + // Match the regular expression below and capture its match into backreference number 2
"\\." + // Match the character “.” literally
"." + // Match any single character that is not a line break character
"+" + // Between one and unlimited times, as many times as possible, giving back as needed (greedy)
")?" + // Between zero and one times, as many times as possible, giving back as needed (greedy)
"$" + // Assert position at the end of a line (at the end of the string or before a line break character)
")" +
"(?!" + // Assert that it is impossible to match the regex below starting at this position (negative lookahead)
"." + // Match any single character that is not a line break character
" " + // Match the character “ ” literally
"*" + // Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
"\\s" + // Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
"\\s" + // Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
")" +
"[^\\x00-\\x1f\\\\?*:\\\"\"><|/]" + // Match a single character NOT present in the list below
// A character in the range between ASCII character 0x00 (0 decimal) and ASCII character 0x1f (31 decimal)
// A \ character
// One of the characters “?*:”
// A " character
// One of the characters “"><|/”
"+" + // Between one and unlimited times, as many times as possible, giving back as needed (greedy)
"$" // Assert position at the end of a line (at the end of the string or before a line break character)
Stämmer detta?
zinoMedlem sedan jan. 200247 inlägg Bra fråga ;) Men, det är det som jag använder och det fungerar som det är bortsett ifrån att tecknen # % & + inte sparas korrekt i filnamnet.
Ja det är ett filnamn som ska valideras med detta. RegExp satsen ska tillåta alla vanliga tecken som bokstäver ifrån diverse språk och sifrror, men inte tillåta följande:
?*:\""""><|/
och inte heller tillåta
& # % +
Mycket möjligt att jag behöver ta bort en del i satsen. Jag har provat följande:
regEx.Pattern = "^[a-zA-Z0-9_åäöÅÄÖæÆøØüÜß]+$" vilket ju tillåter bokstäver och siffror för oss i norra europa. Den tillåter inga andra tecken vilket är bra, men ignorerar fortfarande:
& # %
Däremot tillåter den inte + vilket är bra.
Borde jag inte kunna lägga till i den senare RegExp versionen att den inte ska tillåta
& # %
Hjälpte denna information?
^[\w_åäöæøüß\.]+$
eller(Du vill ha punkt med i dina filnamn också?)
^[\w_åäöæøüß\. ]+$
Ta alla EU tecken. Ville du ha att & # och % skall finnas med så:
^[\w_åäöæøüß\.[b]&#%[/b] ]+$
(och du ha regExp.IgnoreCase = True)
zinoMedlem sedan jan. 200247 inlägg Ja, ignorecase finns med. & # % + skall dock inte vara tillåtna.
Jag börjar luta åt att det är någon annan kod utanför RegExp sektionen som stör. Jag skalade ner all kod så att enbart RegExp sektionen fanns kvar och då fungerar det som det är tänkt. Med, andra ord måste det vara ett fel någon annanstans.
Jag var för insnöad på RegExp så jag tog förgivet att felet låg där.
Ska gå igenom all övrig kod nu så får vi se. Återkommer om jag har fel. ;-)
Tackar för hjälpen i vilket fall som helst.
zinoMedlem sedan jan. 200247 inlägg Lösningen funnen i JavaScript-forumet :)
Lösning