Afficher un message
Vieux 17/08/2006, 13h57   #5
Stephane Chazelas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Regular expression issue in korn shell

On 17 Aug 2006 03:30:31 -0700, Kev wrote:
> Very good, that works!
>
> Could you define a "globbing pattern"? Is it the ksh equivalent of a
> regexp?
>
> Interestingly, I have another regular expression in my script, and this
> seems to work fine:
> *[!0-9]*
>
> As I say, I am new to ksh!

[...]

To my mind, it doesn't make sense to use ksh for scripting. Best
is to write a script in a standard syntax.

*[!0-9]* is a globbing/wildcard/filename expansion/filename
generation/fnmatch pattern (however you want to call it).

The regexp equivalent is ^.*[^0-9].*$ (or [^0-9]).

ksh globbing patterns extend the Bourne or fnmatch patterns
(knowing only ?, * and [...]) with additional operators that
make it functionnaly (but not syntactically) equivalent to
regular expressions (maybe not perl ones though).

There is even some way to convert from those patterns to some
AT&T regular expressions (yet another variant of regular
expressions).

$ ksh93 -c 'printf "%R\n" "*[!0-9]*"'
[^0-9]

$ ksh93 -c 'printf "%R\n" "!(foo)"'
^(foo)!$

(you can see that this (foo)! is not a standard (nor perl) RE
operator).

ksh also provides the reverse operator (converts from regexp to
wildcard):

$ ksh93 -c 'printf "%P\n" "([0-9]+\.){3}"'
*{3}(+([0-9])\.)*

In your case, can you not simply use standard commands:

Something like

if awk '
BEGIN {
if (split(ARGV[1], list, /[.]/) != 4) exit(1)
for (i in list) {
if (list[i] !~ /^[0-9]+$/ || list[i] ~ /^0./) exit(1)
if (list[i] > 255) exit (1)
}
exit(0)
}' "$1"
then

or:

isIP() {
(
IFS=.
a=$1.0
set -f
set -- $a
[ "$#" -ne 5 ] && return 1
for i do
case $i in
*[!0-9]* | "" | 0?*) return 1;;
esac
[ "$i" -gt 255 ] && return 1
done
return 0
)
}
if isIP "$1"
....


--
Stephane
  Réponse avec citation
 
Page generated in 0,06337 seconds with 9 queries