String als Regulären Ausdruck interprätieren

Coalminer

Mitglied
Hallo,

folgende Problemstellung.
Ich übergeb als Argument einem Script einen regulären ausdruck und verwende ihn dann wie folgt:
Code:
if ( $sLineBuffer =~ /$sKeyString/ )
das funktioniert auch soweit.

nur würd ich jetzt gerne noch in einem Argument übergeben wie gesucht werden soll:
also so:
Code:
if ( $sLineBuffer =~ /$sKeyString/$opt)
nur funktioniert das nicht
das gibt nen syntax error

so hab ichs auch versucht:
Code:
if ( $sLineBuffer =~ $sRegExKomplett)
das geht aber schon garnicht weil die "Schrägstriche wohl ausserhalb der Variablen sein müssen damit Perl schnallt das wir über Rguläre Ausdrücke reden.
 
Hallo,

du könntest die Modifikatoren auch in den regulären Ausdruck einbinden

http://search.cpan.org/dist/perl/pod/perlre.pod hat gesagt.:
(?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:

Code:
        $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,

Code:
        ( (?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.

Grüße,
Matthias
 
hm ok,
hab ich das jetzt richtig verstanden...

Code:
/(?i)&suchwort/

hatexakt die selbe bedeutung wie

Code:
/&suchwort/i

?
 
Hallo,

warum nicht einfach ausprobieren? :)

Aber um die Spannung wegzunehmen: ja, diese beiden Ausdrücke führen zum selben Ergebnis.

Grüße,
Matthias
 
Zurück