我可以用什么正则表达式来提取名字?

时间:2016-05-17 00:35:05

标签: regex vb.net

我有下面的字符串,我想提取医生的名字。我可以用什么正则表达式来实现这个目标?

$($('#menu-button-label').click(function() {
      if($('#menu-button').is(':checked')) {
            $('#menu-button-label').animate({marginTop: '0%'},500);
         }
  });

4 个答案:

答案 0 :(得分:2)

描述

(?<=:\s).*

Regular expression visualization

此正则表达式将执行以下操作:

  • 在第一个冒号之后找到所有子字符串后跟一个空格

实施例

直播示例

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

示例文字

Doctor    : JOHN A. BROWN

返回匹配

[0] => JOHN A. BROWN

解释

NODE                     EXPLANATION
----------------------------------------------------------------------
  (?<=                     look behind to see if there is:
----------------------------------------------------------------------
    :                        ':'
----------------------------------------------------------------------
    \s                       whitespace (\n, \r, \t, \f, and " ")
----------------------------------------------------------------------
  )                        end of look-behind
----------------------------------------------------------------------
  .*                       any character except \n (0 or more times
                           (matching the most amount possible))
----------------------------------------------------------------------

答案 1 :(得分:0)

正则表达可能有点矫枉过正。 相反,为什么不从原始字符串中取出一个子字符串。

Dim data As String = "Doctor    : JOHN A. BROWN"
Dim name As String = data.Substring(data.IndexOf(":") + 2)

注意,做&#39; + 2&#39;在子字符串的起始索引上跳过&#39; &#39;出现在&#39;:&#39;。

之后

答案 2 :(得分:0)

你可以这样做:

:\s+(.*)
  • :\s+匹配:后跟一个或多个空格

  • (.*)在此之后匹配任何内容,即所需部分进入捕获的组1.现在,\1将为JOHN A. BROWN

Demo

答案 3 :(得分:0)

How about:

^Doctor\s*:\s*(.*)

This will exclude the "Doctor :" field and match anything after the colon.