正则是从字符串过滤日期时间的时间

发布于 2025-01-24 01:04:55 字数 573 浏览 0 评论 0原文

要求:我正在收到带有模板的电子邮件,我需要从电子邮件中过滤一些文本。我将所有电子邮件主体文本转换为字符串。

电子邮件文本看起来像这样:

some body text which I don't need 

Discussion:
Tue 26/04/2022/2:48 PM UTC+10/ ABC User-

TEST description - this should be logged as a comment.  --- This is the part I need

ABC Company Australia | XYZ St | Sydney NSW 2000

我正在考虑拥有一个以下等级,该正则在

  • “讨论”
  • DateTime格式,以“ 26/04/2022/2:48 PM UTC+10/ABC用户 -
  • 下行寻找 下一行直到我们找到这一行 - “ ABC Company Australia | XYZ ST | Sydney NSW 2000”地址

吗?有人可以帮忙言论吗?

tia。

Requirement: I am receiving an email with a template and I need to filter out some text from the email. I am converting all the email body text as a string.

email text looks like this:

some body text which I don't need 

Discussion:
Tue 26/04/2022/2:48 PM UTC+10/ ABC User-

TEST description - this should be logged as a comment.  --- This is the part I need

ABC Company Australia | XYZ St | Sydney NSW 2000

I was thinking of having a regex that looks for

  • A word "Discussion"
  • Next line look for DateTime format with "Tue 26/04/2022/2:48 PM UTC+10/ ABC User-"
  • Pick up the next line until we find this line - "ABC Company Australia | XYZ St | Sydney NSW 2000" address

Is it possible? can someone plz help with regex?

TIA.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蒲公英的约定 2025-01-31 01:04:55

如果OP感兴趣的内容只是足够的内容,那么以下等级已经足够了... <代码>/讨论:\ n [a-za-z] {1,3} \ s+\ d {2} \/\ d {2} \/\ d {4}。 。*)/

const multilineMail = `Discussion:
Tue 26/04/2022/2:48 PM UTC+10/ ABC User-

TEST description - this should be logged as a comment.  --- This is the part I need

ABC Company Australia | XYZ St | Sydney NSW 2000`;


// see ... [https://regex101.com/r/v8FXCA/3]
const regXMailContent =
  /Discussion:\n[a-zA-Z]{1,3}\s+\d{2}\/\d{2}\/\d{4}.*\n+(?<content>.*)/;

console.log(
  regXMailContent.exec(multilineMail)?.groups?.content
);

如果公司的页脚必须完全匹配,则必须像以下内容一样使其成为上述正则派对的一部分。 > /讨论:\ n [a-za-z] {1,3} \ s+\ d {2} \/\ d {2} \/\ d {2} \/\ d {4}。*\ n+(?&lt;内容&gt;。*)\ n+ABC公司澳大利亚\ | xyz st \ |悉尼新南威尔士州2000/

const multilineMail = `Discussion:
Tue 26/04/2022/2:48 PM UTC+10/ ABC User-

TEST description - this should be logged as a comment.  --- This is the part I need

ABC Company Australia | XYZ St | Sydney NSW 2000`;


// see ... [https://regex101.com/r/v8FXCA/4]
const regXMailContent =
  /Discussion:\n[a-zA-Z]{1,3}\s+\d{2}\/\d{2}\/\d{4}.*\n+(?<content>.*)\n+ABC Company Australia \| XYZ St \| Sydney NSW 2000/;

console.log(
  regXMailContent.exec(multilineMail)?.groups?.content
);

如果OP也要保存日期和用户,则将增强最初提供的正则拨号,例如...

const multilineMail = `Discussion:
Tue 26/04/2022/2:48 PM UTC+10/ ABC User-

TEST description - this should be logged as a comment.  --- This is the part I need

ABC Company Australia | XYZ St | Sydney NSW 2000`;


// see ... [https://regex101.com/r/v8FXCA/2]
const regXMailDateAndContent =
  /Discussion:\n(?<date>[a-zA-Z]{1,3}\s+\d{2}\/\d{2}\/\d{4}).*\n+(?<content>.*)/;

// see ... [https://regex101.com/r/v8FXCA/1]
const regXMailDateUserAndContent =
  /Discussion:\n(?<date>[a-zA-Z]{1,3}\s+\d{2}\/\d{2}\/\d{4}\/[^/]+)\/\s*(?<user>.*?)-?\s*\n+(?<content>.*)/;

console.log(
  regXMailDateAndContent.exec(multilineMail)?.groups
);
console.log(
  regXMailDateUserAndContent.exec(multilineMail)?.groups
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

但是,在 的情况下,要提取的内容是多行文本, Regex必须具有公司页脚才能确定正确的匹配。 2ndly提供的正则是... /docuction:\ n [a-za-z ] {1,3} \ s+\ d {2} \/\ d {2} \/\ d {4}。*\ n+(?&lt; content&gt;(? \ | xyz st \ |悉尼新南威尔士州2000/

const multilineMail = `Discussion:
Tue 26/04/2022/2:48 PM UTC+10/ ABC User-

TEST

description - this should be
logged as a comment.  --- This is

the part I need


ABC Company Australia | XYZ St | Sydney NSW 2000`;


// see ... [https://regex101.com/r/v8FXCA/5]
const regXMailMultilineContent =
  /Discussion:\n[a-zA-Z]{1,3}\s+\d{2}\/\d{2}\/\d{4}.*\n+(?<content>(?:.*\n)*)ABC Company Australia \| XYZ St \| Sydney NSW 2000/;

console.log(
  regXMailMultilineContent.exec(multilineMail)?.groups?.content
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

以上所有以上的正则模式均使用 名为cathing groupt

If it was just about the content the OP is interested in, the following regex already is sufficient enough ... /Discussion:\n[a-zA-Z]{1,3}\s+\d{2}\/\d{2}\/\d{4}.*\n+(?<content>.*)/

const multilineMail = `Discussion:
Tue 26/04/2022/2:48 PM UTC+10/ ABC User-

TEST description - this should be logged as a comment.  --- This is the part I need

ABC Company Australia | XYZ St | Sydney NSW 2000`;


// see ... [https://regex101.com/r/v8FXCA/3]
const regXMailContent =
  /Discussion:\n[a-zA-Z]{1,3}\s+\d{2}\/\d{2}\/\d{4}.*\n+(?<content>.*)/;

console.log(
  regXMailContent.exec(multilineMail)?.groups?.content
);

In case the company footer has to match exactly, one has to make it part of the above regex like follows ... /Discussion:\n[a-zA-Z]{1,3}\s+\d{2}\/\d{2}\/\d{4}.*\n+(?<content>.*)\n+ABC Company Australia \| XYZ St \| Sydney NSW 2000/

const multilineMail = `Discussion:
Tue 26/04/2022/2:48 PM UTC+10/ ABC User-

TEST description - this should be logged as a comment.  --- This is the part I need

ABC Company Australia | XYZ St | Sydney NSW 2000`;


// see ... [https://regex101.com/r/v8FXCA/4]
const regXMailContent =
  /Discussion:\n[a-zA-Z]{1,3}\s+\d{2}\/\d{2}\/\d{4}.*\n+(?<content>.*)\n+ABC Company Australia \| XYZ St \| Sydney NSW 2000/;

console.log(
  regXMailContent.exec(multilineMail)?.groups?.content
);

If the OP wants to also save date and user, one would enhance the firstly provided regex like with ...

const multilineMail = `Discussion:
Tue 26/04/2022/2:48 PM UTC+10/ ABC User-

TEST description - this should be logged as a comment.  --- This is the part I need

ABC Company Australia | XYZ St | Sydney NSW 2000`;


// see ... [https://regex101.com/r/v8FXCA/2]
const regXMailDateAndContent =
  /Discussion:\n(?<date>[a-zA-Z]{1,3}\s+\d{2}\/\d{2}\/\d{4}).*\n+(?<content>.*)/;

// see ... [https://regex101.com/r/v8FXCA/1]
const regXMailDateUserAndContent =
  /Discussion:\n(?<date>[a-zA-Z]{1,3}\s+\d{2}\/\d{2}\/\d{4}\/[^/]+)\/\s*(?<user>.*?)-?\s*\n+(?<content>.*)/;

console.log(
  regXMailDateAndContent.exec(multilineMail)?.groups
);
console.log(
  regXMailDateUserAndContent.exec(multilineMail)?.groups
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

But in case the to be extracted content is a multiline text, the regex has to feature the company footer in order to identify the correct match. The 2ndly provided regex then changes to ... /Discussion:\n[a-zA-Z]{1,3}\s+\d{2}\/\d{2}\/\d{4}.*\n+(?<content>(?:.*\n)*)ABC Company Australia \| XYZ St \| Sydney NSW 2000/

const multilineMail = `Discussion:
Tue 26/04/2022/2:48 PM UTC+10/ ABC User-

TEST

description - this should be
logged as a comment.  --- This is

the part I need


ABC Company Australia | XYZ St | Sydney NSW 2000`;


// see ... [https://regex101.com/r/v8FXCA/5]
const regXMailMultilineContent =
  /Discussion:\n[a-zA-Z]{1,3}\s+\d{2}\/\d{2}\/\d{4}.*\n+(?<content>(?:.*\n)*)ABC Company Australia \| XYZ St \| Sydney NSW 2000/;

console.log(
  regXMailMultilineContent.exec(multilineMail)?.groups?.content
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

All of the above regex patterns make use of named capturing groups.

我爱人 2025-01-31 01:04:55

您可以尝试以下等级:

Discussion.*?\n+([A-Za-z]+ +(?:\d{2}\/){2}\d{4}\/\d+:\d+ +[^\n]+)(.*)?ABC Company Australia \| XYZ St \| Sydney NSW 2000

说明:

  1. 讨论。
  2. 。*?\ n+不断寻找其他单词,而newlines
  3. ([a-za-z]++(?:\ d {2} \/){2} {2} \ d {4 } \/\ d+:\ d++[^\ n]+)接下来,它会如下所述寻找日期格式。它将全部获取,直到它到达Newline [^\ n+]
  4. (。*)?它将从上一个日期行中获取所有内容
  5. abc Company澳大利亚\ | xyz st \ |悉尼新南威尔士州2000
    每当找到这个时,结论匹配。
  6. 在这里,我将日期格式保持在第1组和身体中
    第2组

demo

来源:

const regex = /Discussion.*?\n+([A-Za-z]+ +(?:\d{2}\/){2}\d{4}\/\d+:\d+ +[^\n]+)(.*)?ABC Company Australia \| XYZ St \| Sydney NSW 2000/gms;

const str = `some body text which I don't need 

Discussion:
Tue 26/04/2022/2:48 PM UTC+10/ ABC User-

TEST description - this should be logged as a comment.  --- This is the part I need

ABC Company Australia | XYZ St | Sydney NSW 2000
`;

var match = regex.exec(str);
if(match!=null){
console.log(match[1]);
console.log(match[2]);
}

You may try this regex:

Discussion.*?\n+([A-Za-z]+ +(?:\d{2}\/){2}\d{4}\/\d+:\d+ +[^\n]+)(.*)?ABC Company Australia \| XYZ St \| Sydney NSW 2000

Explanation:

  1. Discussion.*?\n+ The regex starts from where the String Discussion begins.
  2. .*?\n+ keeps looking for additional word and newlines
  3. ([A-Za-z]+ +(?:\d{2}\/){2}\d{4}\/\d+:\d+ +[^\n]+) next it looks for the date format as you described . It will fetch all until it reaches a newline [^\n+]
  4. (.*)?It will fetch everything from the previous date line
  5. ABC Company Australia \| XYZ St \| Sydney NSW 2000 and will
    conclude matching whenever it finds this.
  6. Here I have kept the Date format line in group 1 and the body you
    need in group 2

Demo

source:

const regex = /Discussion.*?\n+([A-Za-z]+ +(?:\d{2}\/){2}\d{4}\/\d+:\d+ +[^\n]+)(.*)?ABC Company Australia \| XYZ St \| Sydney NSW 2000/gms;

const str = `some body text which I don't need 

Discussion:
Tue 26/04/2022/2:48 PM UTC+10/ ABC User-

TEST description - this should be logged as a comment.  --- This is the part I need

ABC Company Australia | XYZ St | Sydney NSW 2000
`;

var match = regex.exec(str);
if(match!=null){
console.log(match[1]);
console.log(match[2]);
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文