【问题标题】:How to match a pattern after particular string?如何在特定字符串之后匹配模式?
【发布时间】:2016-06-07 16:49:17
【问题描述】:

如何使用正则表达式匹配某些模式 SomeText

假设我想查找电子邮件地址,那么我应该只得到:

abcd@xy.com
cdf@errf.com

但我不应该收到上面写的电子邮件SomeText,在javascript中使用正则表达式。

我有一个类似这样的文本文件:

在理论计算机科学和形式语言理论中,一个常规的 表达式(有时称为有理表达式)[1][2] 是 定义搜索模式的字符序列,主要用于 模式匹配字符串,或字符串匹配,即“查找和 类似替换的操作。这个概念出现在 1950 年代,当时 美国 abc@cd.com 数学家 Stephen Kleene 将 一种常规语言的描述,并与 Unix 文本处理实用程序 ed,一个编辑器和 grep,一个过滤器。

bfb@dgf.com

一些文本

姓名1/职业1/状态1

abcd@xy.com

正则表达式在计算中非常有用,以至于需要指定的各种系统 正则表达式已经发展为提供基本和扩展标准 语法和句法;现代正则表达式大大增强了标准。 正则表达式处理器可在多个搜索引擎中找到,搜索和 替换几个文字处理器和文本编辑器的对话框,并在 文本处理实用程序的命令行,例如 sed 和 AWK。

姓名2/职业2/状态2

cdf@errf.com

【问题讨论】:

  • 提示:捕获组
  • 但是如何在 SomeText 之后获得所有结果?请解释
  • 使用 indexOf 和子字符串(或拆分)获取 sometext 之后的文本,然后匹配您需要的内容。
  • @Gopalkrishnasudhanshu - 您需要更具体地了解您的问题(是什么让“SomeText”特别,而以下文本“name1/occupation1...”被忽略)以及您所拥有的已经试过了。
  • SomeText 是像子标题一样的特定文本。我在一些文本的下方和上方有很多匹配项。但我只对 SomeText.@Malvolio 下方的匹配电子邮件感兴趣

标签: javascript regex


【解决方案1】:

我还没有找到在“SomeText”之后获取两个电子邮件地址的方法,所以这是我的建议。

去掉关键词之前的所有文字。然后只需对电子邮件地址使用更简单的正则表达式。下面的正则表达式是来自emailregex 的“官方”表达式,但类似 ​​"([\w\d]+@\w+.\w+)" 的东西会很好用,而且更容易理解:)

str = str.substring(str.indexOf("SomeText") + 1);
results = str.match(/^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+)*\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[a-z][a-z])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$/mg);

【讨论】:

    【解决方案2】:

    您可以将replace 与回调一起使用:

    var emails=[];
    
    s.replace(/\bSomeText([\s\S]+)$/, function($0, $1) {
       $1.match(/[^\s@]+@\S+/g).map(function(e){ emails.push(e) });
       return $0;
    })
    
    console.log(emails);
    // ["abcd@xy.com", "cdf@errf.com"]
    

    PS:查找电子邮件地址[^\s@]+@\S+ 的正则表达式在这里非常基本,电子邮件地址可能非常复杂。

    【讨论】:

      【解决方案3】:

      您的解决方案:

      var string   = '\nIn theoretical computer science and formal language theory, a regular expression (sometimes called a rational expression)[1][2] is a sequence of characters that define a search pattern, mainly for use in pattern matching with strings, or string matching, i.e. "find and replace"-like operations. The concept arose in the 1950s, when the American abc@cd.com mathematician Stephen Kleene formalized the description of a regular language, and came into common use with the Unix text processing utilities ed, an editor, and grep, a filter.\n\nbfb@dgf.com\n\nSomeText\n\nname1/occupation1/state1\n\nabcd@xy.com\n\nRegexps are so useful in computing that the various systems to specify regexps have evolved to provide both a basic and extended standard for the grammar and syntax; modern regexps heavily augment the standard. Regexp processors are found in several search engines, search and replace dialogs of several word processors and text editors, and in the command lines of text processing utilities, such as sed and AWK.\n\nname2/occupation2/state2\n\ncdf@errf.com';
      var someText = 'SomeText';
      var regExp   = new RegExp('\\S+@\\S+\\.\\S+','g');
      var emails   = string.split(someText)[1].match(regExp);
      console.log(emails);
      // ["abcd@xy.com", "cdf@errf.com"]
      

      不要忘记使用您的RegExp 来搜索电子邮件。我提供了最简单的例子。

      【讨论】:

      • 它比@anubhava 解决方案快约 2 倍。
      【解决方案4】:

      您可以执行以下操作

          var str='your text form which you need to find the email ids';
      
          str=str.replace(/\r\n/g,'##') // need to get all the text in one line otherwise your backrefernce will not work.
      
          str=str.replace(/.*sometext(.*)/i,"$1") // remove text before sometext
      
          str.match(/[A-Za-z0-9]+@[A-Za-z]+\.[A-Za-z]+/g)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-07-24
        • 1970-01-01
        • 2021-11-05
        • 1970-01-01
        • 1970-01-01
        • 2017-07-19
        • 2013-06-07
        • 1970-01-01
        相关资源
        最近更新 更多