regexp "case sensitive" ou pas en fonction d'une variable ?
4 réponses
Pierre
Bonjour,
Dans un script CGI écrit en perl, je dois comparer une chaine de caractères
(avec une regexp) soit en tenant compte des majuscules et des minuscules,
soit sans y faire attention. Je voudrais donc pouvoir faire ceci en
fonction de la valeur d'une variable (qui contiendrait soit 'i' soit '').
Comme par exemple dans le code ci-dessous (voir varibale $case). Le
problème c'est que ca ne marche pas :-(
my $str = "Hello World!";
my $pattern "hello";
my $case = "i";
if ($str =~ s/${pattern}/${case}) {
print "match!\n";
}
Cette action est irreversible, confirmez la suppression du commentaire ?
Signaler le commentaire
Veuillez sélectionner un problème
Nudité
Violence
Harcèlement
Fraude
Vente illégale
Discours haineux
Terrorisme
Autre
Jacques Caron
Salut,
On Mon, 24 Oct 2005 09:45:38 +0200, Pierre <pyu-at-belbone.be> wrote:
Dans un script CGI écrit en perl, je dois comparer une chaine de caractères (avec une regexp) soit en tenant compte des majuscules et des minuscules, soit sans y faire attention. Je voudrais donc pouvoir faire ceci en fonction de la valeur d'une variable (qui contiendrait soit 'i' soit ''). Comme par exemple dans le code ci-dessous (voir varibale $case). Le problème c'est que ca ne marche pas :-(
my $str = "Hello World!"; my $pattern "hello"; my $case = "i";
if ($str =~ s/${pattern}/${case}) { print "match!n"; }
Qqn peut m'aider ?
man perlre nous dit:
"(?imsx-imsx)" One or more embedded pattern-match modifiers, to be turned on (or turned off, if preceded by "-") for the remainder of the pattern or the remainder of the enclosing pattern group (if any). This is particularly useful for dynamic patterns, such as those read in from a configuration file, read in as an argument, are specified in a table somewhere, etc. Consider the case that some of which want to be case sensitive and some do not. The case insensitive ones need to include merely "(?i)" at the front of the pattern. For example:
$pattern = "foobar"; if ( /$pattern/i ) { }
# more flexible:
$pattern = "(?i)foobar"; if ( /$pattern/ ) { }
These modifiers are restored at the end of the enclosing group. For example,
( (?i) blah ) s+ 1
will match a repeated (including the case!) word "blah" in any case, assuming "x" modifier, and no "i" modifier outside this group.
"(?:pattern)"
Jacques. -- Oxado http://www.oxado.com/
Salut,
On Mon, 24 Oct 2005 09:45:38 +0200, Pierre <pyu-at-belbone.be> wrote:
Dans un script CGI écrit en perl, je dois comparer une chaine de
caractères
(avec une regexp) soit en tenant compte des majuscules et des minuscules,
soit sans y faire attention. Je voudrais donc pouvoir faire ceci en
fonction de la valeur d'une variable (qui contiendrait soit 'i' soit '').
Comme par exemple dans le code ci-dessous (voir varibale $case). Le
problème c'est que ca ne marche pas :-(
my $str = "Hello World!";
my $pattern "hello";
my $case = "i";
if ($str =~ s/${pattern}/${case}) {
print "match!n";
}
Qqn peut m'aider ?
man perlre nous dit:
"(?imsx-imsx)"
One or more embedded pattern-match modifiers, to be
turned on
(or turned off, if preceded by "-") for the remainder of
the
pattern or the remainder of the enclosing pattern group
(if
any). This is particularly useful for dynamic patterns,
such
as those read in from a configuration file, read in as an
argument, are specified in a table somewhere, etc.
Consider
the case that some of which want to be case sensitive and
some do not. The case insensitive ones need to include
merely "(?i)" at the front of the pattern. For example:
$pattern = "foobar";
if ( /$pattern/i ) { }
# more flexible:
$pattern = "(?i)foobar";
if ( /$pattern/ ) { }
These modifiers are restored at the end of the enclosing
group. For example,
( (?i) blah ) s+ 1
will match a repeated (including the case!) word "blah" in
any case, assuming "x" modifier, and no "i" modifier
outside
this group.
On Mon, 24 Oct 2005 09:45:38 +0200, Pierre <pyu-at-belbone.be> wrote:
Dans un script CGI écrit en perl, je dois comparer une chaine de caractères (avec une regexp) soit en tenant compte des majuscules et des minuscules, soit sans y faire attention. Je voudrais donc pouvoir faire ceci en fonction de la valeur d'une variable (qui contiendrait soit 'i' soit ''). Comme par exemple dans le code ci-dessous (voir varibale $case). Le problème c'est que ca ne marche pas :-(
my $str = "Hello World!"; my $pattern "hello"; my $case = "i";
if ($str =~ s/${pattern}/${case}) { print "match!n"; }
Qqn peut m'aider ?
man perlre nous dit:
"(?imsx-imsx)" One or more embedded pattern-match modifiers, to be turned on (or turned off, if preceded by "-") for the remainder of the pattern or the remainder of the enclosing pattern group (if any). This is particularly useful for dynamic patterns, such as those read in from a configuration file, read in as an argument, are specified in a table somewhere, etc. Consider the case that some of which want to be case sensitive and some do not. The case insensitive ones need to include merely "(?i)" at the front of the pattern. For example:
$pattern = "foobar"; if ( /$pattern/i ) { }
# more flexible:
$pattern = "(?i)foobar"; if ( /$pattern/ ) { }
These modifiers are restored at the end of the enclosing group. For example,
( (?i) blah ) s+ 1
will match a repeated (including the case!) word "blah" in any case, assuming "x" modifier, and no "i" modifier outside this group.
"(?:pattern)"
Jacques. -- Oxado http://www.oxado.com/
Pierre
J'connaissais pas... merci!
"Jacques Caron" wrote in message news:
Salut,
On Mon, 24 Oct 2005 09:45:38 +0200, Pierre <pyu-at-belbone.be> wrote:
Dans un script CGI écrit en perl, je dois comparer une chaine de caractères (avec une regexp) soit en tenant compte des majuscules et des minuscules,
soit sans y faire attention. Je voudrais donc pouvoir faire ceci en fonction de la valeur d'une variable (qui contiendrait soit 'i' soit '').
Comme par exemple dans le code ci-dessous (voir varibale $case). Le problème c'est que ca ne marche pas :-(
my $str = "Hello World!"; my $pattern "hello"; my $case = "i";
if ($str =~ s/${pattern}/${case}) { print "match!n"; }
Qqn peut m'aider ?
man perlre nous dit:
"(?imsx-imsx)" One or more embedded pattern-match modifiers, to be turned on (or turned off, if preceded by "-") for the remainder of the pattern or the remainder of the enclosing pattern group (if any). This is particularly useful for dynamic patterns, such as those read in from a configuration file, read in as an
argument, are specified in a table somewhere, etc. Consider the case that some of which want to be case sensitive and
some do not. The case insensitive ones need to include merely "(?i)" at the front of the pattern. For example:
$pattern = "foobar"; if ( /$pattern/i ) { }
# more flexible:
$pattern = "(?i)foobar"; if ( /$pattern/ ) { }
These modifiers are restored at the end of the enclosing group. For example,
( (?i) blah ) s+ 1
will match a repeated (including the case!) word "blah" in
any case, assuming "x" modifier, and no "i" modifier outside this group.
"(?:pattern)"
Jacques. -- Oxado http://www.oxado.com/
J'connaissais pas... merci!
"Jacques Caron" <jc@oxado.com> wrote in message
news:op.sy44cyicw8m2cx@news.free.fr...
Salut,
On Mon, 24 Oct 2005 09:45:38 +0200, Pierre <pyu-at-belbone.be> wrote:
Dans un script CGI écrit en perl, je dois comparer une chaine de
caractères
(avec une regexp) soit en tenant compte des majuscules et des
minuscules,
soit sans y faire attention. Je voudrais donc pouvoir faire ceci en
fonction de la valeur d'une variable (qui contiendrait soit 'i' soit
'').
Comme par exemple dans le code ci-dessous (voir varibale $case). Le
problème c'est que ca ne marche pas :-(
my $str = "Hello World!";
my $pattern "hello";
my $case = "i";
if ($str =~ s/${pattern}/${case}) {
print "match!n";
}
Qqn peut m'aider ?
man perlre nous dit:
"(?imsx-imsx)"
One or more embedded pattern-match modifiers, to be
turned on
(or turned off, if preceded by "-") for the remainder of
the
pattern or the remainder of the enclosing pattern group
(if
any). This is particularly useful for dynamic patterns,
such
as those read in from a configuration file, read in as
an
argument, are specified in a table somewhere, etc.
Consider
the case that some of which want to be case sensitive
and
some do not. The case insensitive ones need to include
merely "(?i)" at the front of the pattern. For example:
$pattern = "foobar";
if ( /$pattern/i ) { }
# more flexible:
$pattern = "(?i)foobar";
if ( /$pattern/ ) { }
These modifiers are restored at the end of the enclosing
group. For example,
( (?i) blah ) s+ 1
will match a repeated (including the case!) word "blah"
in
any case, assuming "x" modifier, and no "i" modifier
outside
this group.
On Mon, 24 Oct 2005 09:45:38 +0200, Pierre <pyu-at-belbone.be> wrote:
Dans un script CGI écrit en perl, je dois comparer une chaine de caractères (avec une regexp) soit en tenant compte des majuscules et des minuscules,
soit sans y faire attention. Je voudrais donc pouvoir faire ceci en fonction de la valeur d'une variable (qui contiendrait soit 'i' soit '').
Comme par exemple dans le code ci-dessous (voir varibale $case). Le problème c'est que ca ne marche pas :-(
my $str = "Hello World!"; my $pattern "hello"; my $case = "i";
if ($str =~ s/${pattern}/${case}) { print "match!n"; }
Qqn peut m'aider ?
man perlre nous dit:
"(?imsx-imsx)" One or more embedded pattern-match modifiers, to be turned on (or turned off, if preceded by "-") for the remainder of the pattern or the remainder of the enclosing pattern group (if any). This is particularly useful for dynamic patterns, such as those read in from a configuration file, read in as an
argument, are specified in a table somewhere, etc. Consider the case that some of which want to be case sensitive and
some do not. The case insensitive ones need to include merely "(?i)" at the front of the pattern. For example:
$pattern = "foobar"; if ( /$pattern/i ) { }
# more flexible:
$pattern = "(?i)foobar"; if ( /$pattern/ ) { }
These modifiers are restored at the end of the enclosing group. For example,
( (?i) blah ) s+ 1
will match a repeated (including the case!) word "blah" in
any case, assuming "x" modifier, and no "i" modifier outside this group.
"(?:pattern)"
Jacques. -- Oxado http://www.oxado.com/
Pierre
Encore une question:
lorsque le "case sensitive" est désactivé (ignore case), la regexp ne fonctionne pas quand il y a des accents...
Par exemple si j'ai une chaîne de caractère "c'est l'été!" et que je fais une recherche sur "été" ca ne marche pas quand l'option "i" est présente.
$pattern = "(?i)été"; if ( /$pattern/ ) { }
Est-ce qu'il y a moyen de faire fonctionner ceci ????
"Jacques Caron" wrote in message news:
Salut,
On Mon, 24 Oct 2005 09:45:38 +0200, Pierre <pyu-at-belbone.be> wrote:
Dans un script CGI écrit en perl, je dois comparer une chaine de caractères (avec une regexp) soit en tenant compte des majuscules et des minuscules,
soit sans y faire attention. Je voudrais donc pouvoir faire ceci en fonction de la valeur d'une variable (qui contiendrait soit 'i' soit '').
Comme par exemple dans le code ci-dessous (voir varibale $case). Le problème c'est que ca ne marche pas :-(
my $str = "Hello World!"; my $pattern "hello"; my $case = "i";
if ($str =~ s/${pattern}/${case}) { print "match!n"; }
Qqn peut m'aider ?
man perlre nous dit:
"(?imsx-imsx)" One or more embedded pattern-match modifiers, to be turned on (or turned off, if preceded by "-") for the remainder of the pattern or the remainder of the enclosing pattern group (if any). This is particularly useful for dynamic patterns, such as those read in from a configuration file, read in as an
argument, are specified in a table somewhere, etc. Consider the case that some of which want to be case sensitive and
some do not. The case insensitive ones need to include merely "(?i)" at the front of the pattern. For example:
$pattern = "foobar"; if ( /$pattern/i ) { }
# more flexible:
$pattern = "(?i)foobar"; if ( /$pattern/ ) { }
These modifiers are restored at the end of the enclosing group. For example,
( (?i) blah ) s+ 1
will match a repeated (including the case!) word "blah" in
any case, assuming "x" modifier, and no "i" modifier outside this group.
"(?:pattern)"
Jacques. -- Oxado http://www.oxado.com/
Encore une question:
lorsque le "case sensitive" est désactivé (ignore case), la regexp ne
fonctionne pas quand il y a des accents...
Par exemple si j'ai une chaîne de caractère "c'est l'été!" et que je fais
une recherche sur "été" ca ne marche pas quand l'option "i" est présente.
$pattern = "(?i)été";
if ( /$pattern/ ) { }
Est-ce qu'il y a moyen de faire fonctionner ceci ????
"Jacques Caron" <jc@oxado.com> wrote in message
news:op.sy44cyicw8m2cx@news.free.fr...
Salut,
On Mon, 24 Oct 2005 09:45:38 +0200, Pierre <pyu-at-belbone.be> wrote:
Dans un script CGI écrit en perl, je dois comparer une chaine de
caractères
(avec une regexp) soit en tenant compte des majuscules et des
minuscules,
soit sans y faire attention. Je voudrais donc pouvoir faire ceci en
fonction de la valeur d'une variable (qui contiendrait soit 'i' soit
'').
Comme par exemple dans le code ci-dessous (voir varibale $case). Le
problème c'est que ca ne marche pas :-(
my $str = "Hello World!";
my $pattern "hello";
my $case = "i";
if ($str =~ s/${pattern}/${case}) {
print "match!n";
}
Qqn peut m'aider ?
man perlre nous dit:
"(?imsx-imsx)"
One or more embedded pattern-match modifiers, to be
turned on
(or turned off, if preceded by "-") for the remainder of
the
pattern or the remainder of the enclosing pattern group
(if
any). This is particularly useful for dynamic patterns,
such
as those read in from a configuration file, read in as
an
argument, are specified in a table somewhere, etc.
Consider
the case that some of which want to be case sensitive
and
some do not. The case insensitive ones need to include
merely "(?i)" at the front of the pattern. For example:
$pattern = "foobar";
if ( /$pattern/i ) { }
# more flexible:
$pattern = "(?i)foobar";
if ( /$pattern/ ) { }
These modifiers are restored at the end of the enclosing
group. For example,
( (?i) blah ) s+ 1
will match a repeated (including the case!) word "blah"
in
any case, assuming "x" modifier, and no "i" modifier
outside
this group.
lorsque le "case sensitive" est désactivé (ignore case), la regexp ne fonctionne pas quand il y a des accents...
Par exemple si j'ai une chaîne de caractère "c'est l'été!" et que je fais une recherche sur "été" ca ne marche pas quand l'option "i" est présente.
$pattern = "(?i)été"; if ( /$pattern/ ) { }
Est-ce qu'il y a moyen de faire fonctionner ceci ????
"Jacques Caron" wrote in message news:
Salut,
On Mon, 24 Oct 2005 09:45:38 +0200, Pierre <pyu-at-belbone.be> wrote:
Dans un script CGI écrit en perl, je dois comparer une chaine de caractères (avec une regexp) soit en tenant compte des majuscules et des minuscules,
soit sans y faire attention. Je voudrais donc pouvoir faire ceci en fonction de la valeur d'une variable (qui contiendrait soit 'i' soit '').
Comme par exemple dans le code ci-dessous (voir varibale $case). Le problème c'est que ca ne marche pas :-(
my $str = "Hello World!"; my $pattern "hello"; my $case = "i";
if ($str =~ s/${pattern}/${case}) { print "match!n"; }
Qqn peut m'aider ?
man perlre nous dit:
"(?imsx-imsx)" One or more embedded pattern-match modifiers, to be turned on (or turned off, if preceded by "-") for the remainder of the pattern or the remainder of the enclosing pattern group (if any). This is particularly useful for dynamic patterns, such as those read in from a configuration file, read in as an
argument, are specified in a table somewhere, etc. Consider the case that some of which want to be case sensitive and
some do not. The case insensitive ones need to include merely "(?i)" at the front of the pattern. For example:
$pattern = "foobar"; if ( /$pattern/i ) { }
# more flexible:
$pattern = "(?i)foobar"; if ( /$pattern/ ) { }
These modifiers are restored at the end of the enclosing group. For example,
( (?i) blah ) s+ 1
will match a repeated (including the case!) word "blah" in
any case, assuming "x" modifier, and no "i" modifier outside this group.
"(?:pattern)"
Jacques. -- Oxado http://www.oxado.com/
Jacques Caron
Salut,
On Mon, 24 Oct 2005 13:45:46 +0200, Pierre <pyu-at-belbone.be> wrote:
Encore une question:
lorsque le "case sensitive" est désactivé (ignore case), la regexp ne fonctionne pas quand il y a des accents...
Par exemple si j'ai une chaîne de caractère "c'est l'été!" et que je fais une recherche sur "été" ca ne marche pas quand l'option "i" est présente.
$pattern = "(?i)été"; if ( /$pattern/ ) { }
Est-ce qu'il y a moyen de faire fonctionner ceci ????
Il faudrait plus de détails sur: - la version de perl utilisée - si c'est de l'UTF-8 ou pas, et sinon, quel encodage (pour la chaîne matchée et pour le source perl, éventuellement) - quelle est la locale en vigueur - l'utilisation ou pas de "use bytes", "use utf8", "use locale", "use encoding" et probablement quelques autres...
Jacques. -- Oxado http://www.oxado.com/
Salut,
On Mon, 24 Oct 2005 13:45:46 +0200, Pierre <pyu-at-belbone.be> wrote:
Encore une question:
lorsque le "case sensitive" est désactivé (ignore case), la regexp ne
fonctionne pas quand il y a des accents...
Par exemple si j'ai une chaîne de caractère "c'est l'été!" et que je fais
une recherche sur "été" ca ne marche pas quand l'option "i" est présente.
$pattern = "(?i)été";
if ( /$pattern/ ) { }
Est-ce qu'il y a moyen de faire fonctionner ceci ????
Il faudrait plus de détails sur:
- la version de perl utilisée
- si c'est de l'UTF-8 ou pas, et sinon, quel encodage (pour la chaîne
matchée et pour le source perl, éventuellement)
- quelle est la locale en vigueur
- l'utilisation ou pas de "use bytes", "use utf8", "use locale", "use
encoding" et probablement quelques autres...
On Mon, 24 Oct 2005 13:45:46 +0200, Pierre <pyu-at-belbone.be> wrote:
Encore une question:
lorsque le "case sensitive" est désactivé (ignore case), la regexp ne fonctionne pas quand il y a des accents...
Par exemple si j'ai une chaîne de caractère "c'est l'été!" et que je fais une recherche sur "été" ca ne marche pas quand l'option "i" est présente.
$pattern = "(?i)été"; if ( /$pattern/ ) { }
Est-ce qu'il y a moyen de faire fonctionner ceci ????
Il faudrait plus de détails sur: - la version de perl utilisée - si c'est de l'UTF-8 ou pas, et sinon, quel encodage (pour la chaîne matchée et pour le source perl, éventuellement) - quelle est la locale en vigueur - l'utilisation ou pas de "use bytes", "use utf8", "use locale", "use encoding" et probablement quelques autres...