如何在任何运算符之间提取字符串?

时间:2016-03-05 06:27:23

标签: regex perl

我有一个包含@arr = { "a=b", "a>b", "a<b", "a!=b", "a-b" }的数组。什么是与任何运营商之间获得a和b的最佳方式。我可以通过

提取
for($i=0; $i<=$#arr; $i++){
   $str = $arr[$i];
   if($str =~ m/^(.*?)(\s*=\s*)(.*)(;)/g){
         my $d = $1;
         my $e = $3;
   }

跟随所有if语句,使用可能的运算符,如"!=", "<"等。但这会使我的代码看起来很乱。对此更好的解决方案吗?

4 个答案:

答案 0 :(得分:0)

一个非常简单的正则表达式可能是

/^(\w+)\s*(\W+)\s*(\w+)$/

或者您枚举可能的运算符

   /^(\w+)\s*(=|!=|<|>|<=|>=|\+|-|\*|\/|==)\s*(\w+)$/

这取决于输入是否可信。如果没有,你可能必须更加细致w.r.t.标识符也是。这是一个更简单的循环,不需要使用m // g(lobal)。不确定分号 - 省略它。

my @arr = ( "a=b", "a>b", "a<b", "a!=b", "a-b" );
for my $str (@arr){
    if($str =~ /^(\w+)\s*(=|!=|<|>|<=|>=|\+|-|\*|\/|==)\s*(\w+)$/ ){
         my $d = $1;
         my $e = $3;
         print "d=$d e=$e\n";
    }
}

稍后如果您枚举运算符,还可以添加单词符号:

if($str =~ /^(\w+)\s*(=|!=|<|>|<=|>=|\+|-|\*|\/|==|x?or|and)\s*(\w+)$/ ){
    ...

答案 1 :(得分:0)

您可以尝试类似这样的内容

 MainPage = new ContentPage
            {
                BackgroundImage = "background.png",
                Content = new StackLayout
                {
                    VerticalOptions = LayoutOptions.CenterAndExpand,
                    HorizontalOptions = LayoutOptions.CenterAndExpand,
                    Spacing = 50,
                    Children = {

                        new Label {
                            HorizontalTextAlignment = TextAlignment.Center,
                            Text = "Welcome, Please Sign in!",
                            FontSize=50,
                            TextColor=Color.Gray,
                        },


                         new Entry
                        {
                             Placeholder="Username",
                            VerticalOptions = LayoutOptions.Center,
                            Keyboard = Keyboard.Text,
                            HorizontalOptions = LayoutOptions.Center,
                             WidthRequest = 350,
                             HeightRequest = 50,
                             FontSize=20,
                             TextColor=Color.Gray,
                             PlaceholderColor=Color.Gray,
                        },

                          new Entry
                        {
                             Placeholder="Password",
                            VerticalOptions = LayoutOptions.Center,

                            Keyboard = Keyboard.Text,
                            HorizontalOptions = LayoutOptions.Center,
                             WidthRequest = 350,
                             HeightRequest = 50,
                             FontSize=25,
                             TextColor=Color.Gray,
                             IsPassword=true,
                              PlaceholderColor =Color.Gray,
                        },
                        new Button
                        {
                            Text="Login",
                            FontSize=Device.GetNamedSize(NamedSize.Large,typeof(Button)),
                            HorizontalOptions=LayoutOptions.Center,
                            VerticalOptions=LayoutOptions.Fill,
                            WidthRequest=350,
                            TextColor=Color.Silver,
                            BackgroundColor=Color.Red,
                            BorderColor=Color.Red,
                        },
                       new Label //for this label I want to create click event to open new page
                        {
                            Text="Forgot Password?",
                            FontSize=20,
                            TextColor=Color.Blue,
                            HorizontalOptions=LayoutOptions.Center,

                        },
                    } 
        }
            };

关键是贪婪的匹配,即两个单字符匹配之间的“(。*)”即“(。)”。要确保从字符串的开头和结尾开始,可以使用此

perl -e '@a = ("a=b","a>b","a<b","a!=b","a-b"); for $l (@a) { $l =~ s/(.).*(.)/$1/; print "$1$2\n"};'

一个完整的工作示例,演示了整个事情

perl -e '@a = ("a=b","a>b","a<b","a!=b","a-b"); for $l (@a) { $l =~ s/^(.).*(.)$/$1/; print "$1$2\n"};'

答案 2 :(得分:0)

如果总是&#39; a&#39;和&#39; b&#39;在开始和结束时你可以尝试:

my $str =  'a<b';
my( $op ) = $str =~ /^a(.*)b$/;

答案 3 :(得分:0)

没有经过深思熟虑的答案。将重新考虑这个问题。