簡體   English   中英

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

[英]How to match a pattern after particular string?

如何在 SomeText 之后使用正則表達式匹配某些模式

假設我想查找電子郵件地址,那么我應該只得到:

abcd@xy.com
cdf@errf.com

但是我不應該在javascript中使用正則表達式來獲取在SomeText上方編寫的電子郵件。

我有一個類似這樣的文本文件:

在理論計算機科學和形式語言理論中,正則表達式(有時稱為有理表達式)[1] [2]是定義搜索模式的字符序列,主要用於與字符串或字符串匹配的模式匹配,即類似於“查找並替換”的操作。 這個概念出現在1950年代,當時美國的abc@cd.com數學家Stephen Kleene正式化了對常規語言的描述,並與Unix文本處理實用程序ed(一個編輯器)和grep(一個過濾器)共同使用。

bfb@dgf.com

SomeText

名稱1 /職業1 /狀態1

abcd@xy.com

正則表達式在計算中非常有用,以至於各種用於指定正則表達式的系統已經發展成為語法和語法的基本和擴展標准。 現代正則表達式極大地提高了標准。 正則表達式處理器可在多個搜索引擎中找到,幾個文字處理器和文本編輯器的搜索和替換對話框以及文本處理實用程序(如sed和AWK)的命令行中都有。

名稱2 /職業2 /狀態2

cdf@errf.com

您可以使用帶有回調的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+正則表達式在這里非常基本,電子郵件地址可能非常復雜。

您的解決方案:

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搜索電子郵件。 我提供了最簡單的示例。

我還沒有找到在“ 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);

您可以執行以下操作

    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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM