|
Joined: Feb 2004
Posts: 119
Vogon poet
|
OP
Vogon poet
Joined: Feb 2004
Posts: 119 |
To check check a string if there is no comma i use this right?
if (, !isin $1) { }
but how the i make it check if "before and after the comma" there is no space? like "testing,testing" and not "testing, testing" or "testing ,testing" or any other similar combination???
|
|
|
|
Joined: Nov 2003
Posts: 2,321
Hoopy frood
|
Hoopy frood
Joined: Nov 2003
Posts: 2,321 |
$regex($1-,/\S $+ $chr(44) $+ \S/)
|
|
|
|
Joined: Feb 2004
Posts: 119
Vogon poet
|
OP
Vogon poet
Joined: Feb 2004
Posts: 119 |
when i add
if $regex($1-,/\S $+ $chr(44) $+ \S/) { } it don't give the result i want.
I need to like this.. if (, !isin $1) { run command } where if there is a space before a comma, after it.. or both... it will halt.
Or did i use $regex wrongly? (me not used to $regex)
|
|
|
|
Joined: Nov 2003
Posts: 2,321
Hoopy frood
|
Hoopy frood
Joined: Nov 2003
Posts: 2,321 |
var %i = 1, %err
while $pos($1-,$chr(44),%i) {
if $mid($1-,$calc($pos($1-,$chr(44),%i) -1),1) == $chr(32) || $mid($1-,$calc($pos($1-,$chr(44),%i) +1),1) == $chr(32) { %err = 1 }
inc %i
}
if !%err { dostuff }
|
|
|
|
Joined: Feb 2004
Posts: 2,013
Hoopy frood
|
Hoopy frood
Joined: Feb 2004
Posts: 2,013 |
Hi, the following is slightly better (not being picky, huh tidy :tongue:) because it will immediately halt your script when it matched a space before or after the comma, where in tidy's script it will keep processing, until all commas were found. Also i use var %y = $ifmatch, so that your script doesnt need to look up the position again, it's stored in a variable, which is also slighly faster. Either way works though
var %x = 1, %y
while $pos($1-,$chr(44),%x) {
%y = $ifmatch
if $mid($1-,$calc(%y -1),1) == $chr(32) || $mid($1-,$calc(%y +1),1) == $chr(32) { return }
inc %x
}
commands You could make a custom identifier like this:
alias chk.comma {
var %x = 1, %y
while $pos($1-,$chr(44),%x) {
%y = $ifmatch
if $mid($1-,$calc(%y -1),1) == $chr(32) || $mid($1-,$calc(%y +1),1) == $chr(32) { return true }
inc %x
}
} Then simply use this in your script:
if !$chk.comma($1-) { do stuff } Although I'm positive that a well written $regex is surely faster, though I'm not familiar enough with em to make a decent one. Edit:
alias chk.comma var %re = /(\s*\,\s+|\s+\,\s*)/ | if $regex($1-,%re) { return true } Hehe, it worx, but well, im sure it can be optimized a lot. Enjoy
Last edited by FiberOPtics; 25/04/04 06:37 PM.
|
|
|
|
Joined: Dec 2002
Posts: 1,893
Hoopy frood
|
Hoopy frood
Joined: Dec 2002
Posts: 1,893 |
if there is a space before a comma, after it.. //var %text = test, test | if $regex(%text, / \x2C|\x2C /) { echo -a indeed } //var %text = test, test | if $count(%text,$chr(32) $+ $chr(44),$chr(44) $+ $chr(32)) { echo -a indeed }
|
|
|
|
Joined: Aug 2003
Posts: 325
Fjord artisan
|
Fjord artisan
Joined: Aug 2003
Posts: 325 |
try this code: if ( !$wildtok($1-,$+(*?,$chr(44),?*),0,32) ) { return } or if you wish to make an alias/identifier: alias chk, return $iif($wildtok($1-,$+(*?,$chr(44),?*),0,32),$true,$false) then use: if ( !$chk,($1-) ) { return } What's happening? in $wildtok, first the $+(...) is piece the *?, the $chr(44) (the comma) and the ?* together so it's doing a wild search for *?,?*, where the * means anything, and the ? makes at least 1 character required. Since the tokens are spaces (32 as the last parameter), it doesn't count spaces as matching the ? requirement. therefore, SOMETHING must be there. ?,? would require 1 character, a comma and 1 character after that, the *'s at the front and end expand it to allow more. The "0" in the line makes $wildtok to return the total number of matches, and if it's 0, then you have no test,test or whatever,whateverelse in your text.  Whats nice is that you can use $wildtok to get each matching token/item as you need it, if it works with what you are doing.
|
|
|
|
|