正则表达式始终无法正常工作,为什么?

时间:2019-06-04 22:13:48

标签: regex

正则表达式在某些情况下只能工作,而在其他情况下则不能工作。

https://regex101.com/r/p5u3N6/1

我希望正则表达式仅匹配两个“ {} {}”的组,而{}之间没有任何匹配

3 个答案:

答案 0 :(得分:0)

我猜我们只想使用类似于以下的表达式捕获演示中列出的三个输入:

 public function login(Request $request)
{
    $validatedData = $request->validate([
        'email' => 'required',
        'password' => 'required'
    ]);
    $email = $request->email;
    $password = $request->password;
    //If the user checked the remember me checkbox then make sure to remember him
    if(isset($request->rememberMe)) {
        if (\Auth::attempt(['email' => $email, 'password' => $password], true)) {
            $user = User::findOrFail($email);
            //Remember the user
            \Auth::login($user, true);
            return redirect('/home');
        } else {
            //Otherwise redirect back to login page with errors.
            return redirect()->back()->withInput()->withErrors('Invalid email  or password');
        }
        //If the user didn't check the remember me checkbox then don't remember him.
    } else {
        if (\Auth::attempt(['email' => $email, 'password' => $password])) {
            $user = User::findOrFail($email);
            \Auth::login($user);
            return redirect('/home');
        } else {
            //Otherwise redirect back to login page with errors.
            return redirect()->back()->withInput()->withErrors('Invalid email or password');
        }
    }
}

Demo 1

(\{.*?\}(.+?){.*?\}) 

Demo 2

答案 1 :(得分:0)

模式第一部分中的.*?正在遍历输入的意外部分,直到找到为止,因为.接受了所有这些字符。仅用?来使量词变得懒惰是不够的-它会继续进行直到找到匹配项。

\{[^}]*?\}\s\{[^}]*?\}

https://regex101.com/r/p5u3N6/5

答案 2 :(得分:0)

不确定我是否理解您的要求,我想您只希望成对的{}{}匹配,并且在这两个之间只允许一个空格。您可以尝试使用此\{([^\{]+)\}\ \{([^\}]+)\}