如何匹配特定字符串后的模式?

时间:2016-06-07 16:49:17

标签: javascript regex

如何在 SomeText

之后使用正则表达式匹配某些模式

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

abcd@xy.com
cdf@errf.com

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

我有一个像这样的文本文件:

  

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

     

bfb@dgf.com

     

SomeText

     

NAME1 / occupation1 / STATE1

     

abcd@xy.com

     

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

     

NAME2 / occupation2 / STATE2

     

cdf@errf.com

4 个答案:

答案 0 :(得分:1)

您可以将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+的正则表达式非常基本,电子邮件地址可能非常复杂。

答案 1 :(得分:1)

您的解决方案:

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搜索电子邮件。我提供了最简单的例子。

答案 2 :(得分: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);

答案 3 :(得分:0)

您可以执行以下操作

    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)