質問

この問題につかってしました。私の属性の文字列の形式

"x=1 and y=abc and z=c4g and ..."

一部の属性の値としているということでもあるアルファ値をもつものがあり、混合、日程等に

各文字列は いて"x=someval and y=anotherval"初めに、一部の位置づけはどうなるんですか。いつか必要があります。

  1. を検証する文字列れていることを確認できます。い xy.
  2. 実際に構文解析の値 xy.
  3. 下の文字列になります。

たとえば、このように以下の変数:

$x = 1;
$y = "abc";
$remainder = "z=c4g and ..."

私の質問はあり(合理的に)簡単に構文解析され を検証す単一の正規表現とはすなわち:

if ($str =~ /someexpression/)
{
    $x = $1;
    $y = $2;
    $remainder = $3;
}

ご注意この文字列の場合 のみ xy 属性です。これは有効な文字列になります。

私の後の私のとすることによって、答えがないも単regexの優先度を設定する。

役に立ちましたか?

解決

ませんので正規表現がこのよう狭いう探:

/x=(.+) and y=([^ ]+)( and (.*))?/

以外を使用す$1,$2,と$4.使用時:

my @strs = ("x=1 and y=abc and z=c4g and w=v4l",
            "x=yes and y=no",
            "z=nox and w=noy");

foreach (@strs) {
    if ($_ =~ /x=(.+) and y=([^ ]+)( and (.*))?/) {
        $x = $1;
        $y = $2;
        $remainder = $4;
        print "x: $x; y: $y; remainder: $remainder\n";
    } else {
        print "Failed.\n";
    }
}

出力:

x: 1; y: abc; remainder: z=c4g and w=v4l
x: yes; y: no; remainder: 
Failed.

このコースの葉を出しをたっぷりのエラーチェック、ないわからないすべての入力がこのようだ。

他のヒント

とおもいるその他の名前=値のペアがこのようにのんびりとか、使用Perl版5.10):

use 5.10.0;
use strict;
use warnings;

my %hash;
while(
    $string =~ m{
       (?: ^ | \G )    # start of string or previous match
       \s*

       (?<key>   \w+ ) # word characters
       =
       (?<value> \S+ ) # non spaces

       \s*             # get to the start of the next match
       (?: and )?
    }xgi
){
    $hash{$+{key}} = $+{value};
}

# to make sure that x & y exist
die unless exists $hash{x} and exists $hash{y};

旧Perls(少なくともPerl5.6);

use strict;
use warnings;

my %hash;
while(
    $string =~ m{
       (?: ^ | \G )   # start of string or previous match
       \s*

       ( \w+ ) = ( \S+ )

       \s*            # get to the start of the next match
       (?: and )?
    }xgi
){
    $hash{$1} = $2;
}

# to make sure that x & y exist
die unless exists $hash{x} and exists $hash{y};

これらに働き続ける場合って動作する必要があります。

としてかなり簡単な改造にRudd→版

/^x=(.+) and y=([^ ]+)(?: and (.*))?/

利用できるようになります$1,$2$3(同?:でnoncapturingグループは、この文字列で始まx="より能"not_x="合わせ

場合により知識のxとyの値は、この使用を引き締めるregexさ:

my @strs = ("x=1 and y=abc and z=c4g and w=v4l",
        "x=yes and y=no",
        "z=nox and w=noy",
        "not-x=nox and y=present",
        "x=yes and w='there is no and y=something arg here'");

foreach (@strs) {
    if ($_ =~ /^x=(.+) and y=([^ ]+)(?: and (.*))?/) {
        $x = $1;
        $y = $2;
        $remainder = $3;
        print "x: {$x}; y: {$y}; remainder: {$remainder}\n";
    } else {
        print "$_ Failed.\n";
    }
}

出力:

x: {1}; y: {abc}; remainder: {z=c4g and w=v4l}
x: {yes}; y: {no}; remainder: {}
z=nox and w=noy Failed.
not-x=nox and y=present Failed.
x: {yes and w='there is no}; y: {something}; remainder: {}

なお、欠品の最終試験により現在のバージョンの試験が不要な空間の場合、xの試験と同様に制限する文字列という点で共通しています。

Rudd→びCebjyre頂いておりますのそばに住むことによって特定の問題:

Rudd→考:

/x=(.+) とy=([^]+)((.*))?/

Cebjyre変更いたしました

/^x=(.+) y=([^ ]+)(?:および(.*))?/

第二版はありませんが混乱"not_x=foo"と"x=foo"が受け入れなどに"x=foo z=バー y=baz"をセット$1="foo z=バー"をすることは望ましくない。

これが本を探してい:

/^x=(\w+)とy=(\w+)(?:および(.*))?/

こ士の間であれば、x=y=オプション、場所により可能になる操作、およびオプション"と---"とさせていただき$3

こちらは基本的にかかったのでこれを解決する:

($x_str, $y_str, $remainder) = split(/ and /, $str, 3);

if ($x_str !~ /x=(.*)/)
{
    # error
}

$x = $1;

if ($y_str !~ /y=(.*)/)
{
    # error
}

$y = $1;

私は省略の一部追加の検証およびエラー。この技法の作品などの簡潔なやかと思っていた。とを願っていってより良い提案でした。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top