正则是从字符串过滤日期时间的时间
要求:我正在收到带有模板的电子邮件,我需要从电子邮件中过滤一些文本。我将所有电子邮件主体文本转换为字符串。
电子邮件文本看起来像这样:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果OP感兴趣的内容只是足够的内容,那么以下等级已经足够了... <代码>/讨论:\ n [a-za-z] {1,3} \ s+\ d {2} \/\ d {2} \/\ d {4}。 。*)/
如果公司的页脚必须完全匹配,则必须像以下内容一样使其成为上述正则派对的一部分。 >
/讨论:\ n [a-za-z] {1,3} \ s+\ d {2} \/\ d {2} \/\ d {2} \/\ d {4}。*\ n+(?&lt;内容&gt;。*)\ n+ABC公司澳大利亚\ | xyz st \ |悉尼新南威尔士州2000/
如果OP也要保存日期和用户,则将增强最初提供的正则拨号,例如...
但是,在 的情况下,要提取的内容是多行文本, Regex必须具有公司页脚才能确定正确的匹配。 2ndly提供的正则是...
/docuction:\ n [a-za-z ] {1,3} \ s+\ d {2} \/\ d {2} \/\ d {4}。*\ n+(?&lt; content&gt;(? \ | xyz st \ |悉尼新南威尔士州2000/
以上所有以上的正则模式均使用
名为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>.*)/
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/
If the OP wants to also save date and user, one would enhance the firstly provided regex like with ...
/Discussion:\n(?<date>[a-zA-Z]{1,3}\s+\d{2}\/\d{2}\/\d{4}).*\n+(?<content>.*)/
/Discussion:\n(?<date>[a-zA-Z]{1,3}\s+\d{2}\/\d{2}\/\d{4}\/[^/]+)\/\s*(?<user>.*?)-?\s*\n+(?<content>.*)/
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/
All of the above regex patterns make use of
named capturing groups
.您可以尝试以下等级:
说明:
讨论。
。*?\ n+
不断寻找其他单词,而newlines([a-za-z]++(?:\ d {2} \/){2} {2} \ d {4 } \/\ d+:\ d++[^\ n]+)
接下来,它会如下所述寻找日期格式。它将全部获取,直到它到达Newline[^\ n+]
(。*)?
它将从上一个日期行中获取所有内容abc Company澳大利亚\ | xyz st \ |悉尼新南威尔士州2000
和每当找到这个时,结论匹配。
第2组
demo
来源:
You may try this regex:
Explanation:
Discussion.*?\n+
The regex starts from where the StringDiscussion
begins..*?\n+
keeps looking for additional word and newlines([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+]
(.*)?
It will fetch everything from the previous date lineABC Company Australia \| XYZ St \| Sydney NSW 2000
and willconclude matching whenever it finds this.
need in group 2
Demo
source: