正则表达式和照顾可能的空白

时间:2016-04-04 13:58:03

标签: c# regex

我的正则表达式需要帮助。我正在尝试匹配关键字this,但仅限于括号(this)。到目前为止,我有:

(\()\bthis\b(\))

但看起来它也匹配包含单词的括号,而我只需要抓住单词本身。另一个问题是,如果括号内有空格,它将无效:( this )

1 个答案:

答案 0 :(得分:3)

与那种REGEXP表达式匹配的组怎么样:

 [HttpPost]
        public ActionResult Login(LoginClass model, string ReturnUrl)
        {

            if (ModelState.IsValid)
            {
                if (Membership.ValidateUser(model.UserName, model.Password))
                {
                    FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                    if (Url.IsLocalUrl(ReturnUrl) && ReturnUrl.Length > 1 && ReturnUrl.StartsWith("/")
                        && !ReturnUrl.StartsWith("//") && !ReturnUrl.StartsWith("/\\"))
                    {
                       return Redirect(ReturnUrl);
                    }
                    else
                    {
                        return RedirectToAction("Index", "Home");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "The user name or password provided is incorrect");
                }
            }

            return RedirectToAction("Index", "Home");
        }

此外,如果您想在有空格(空格,制表符等)时进行匹配:

\((this)\)

在此处试试:Regex101。我在正则表达式中使用的每个字符的所有详细信息都在该网站上详细说明。

您可以按代码检索组中匹配的\(\s*(this)\s*\) 值。请查看与您正在使用的语言相关的文档。

相关问题