发布于 2025-01-14 14:22:27 字数 804 浏览 0 评论 0原文

I have a timestamp on a video in similar format to Thurs May 7 10:21:02 1998. I'd like to extract this piece of text from the video. Note: The day may be of 3-4 characters (ex. Wed, Thurs) and the date may be of 1-2 characters.

I tried to look for similarly asked questions on this platform but I couldn't find one that uses regex to extract date in this particular format, taking care of the spaces and the changing number of characters for the day and date.

这是我的尝试:

text = pytesseract.image_to_string(Image.open(file))

# date_time = re.findall(r'\d{2}:\d{2}:\d{2}', text) # works fine; extracts the time as desired
date_time = re.findall(r'\d{3,4} \d{3} \d{1,2} \d{2}:\d{2}:\d{2} \d{4}', text) #doesn't work

print ("timestamp: ", date_time)

I have a timestamp on a video in similar format to Thurs May 7 10:21:02 1998. I'd like to extract this piece of text from the video. Note: The day may be of 3-4 characters (ex. Wed, Thurs) and the date may be of 1-2 characters.

I tried to look for similarly asked questions on this platform but I couldn't find one that uses regex to extract date in this particular format, taking care of the spaces and the changing number of characters for the day and date.

Here is my attempt:

text = pytesseract.image_to_string(Image.open(file))

# date_time = re.findall(r'\d{2}:\d{2}:\d{2}', text) # works fine; extracts the time as desired
date_time = re.findall(r'\d{3,4} \d{3} \d{1,2} \d{2}:\d{2}:\d{2} \d{4}', text) #doesn't work

print ("timestamp: ", date_time)

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

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

发布评论

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

评论(1

菊凝晚露 2025-01-21 14:22:27

continue

You can use

\w+\s+\w{3}\s+\d{1,2}\s+\d{2}:\d{2}:\d{2}\s+\d{4}

See the regex demo. Details:

  • \w+ - one or more word chars
  • \s+ - one or more whitespaces
  • \w{3} - three word chars
  • \s+ - one or more whitespaces
  • \d{1,2} - one or two digits
  • \s+ - one or more whitespaces
  • \d{2}:\d{2}:\d{2} - two digits, :, two digits, : and two digits
  • \s+ - one or more whitespaces
  • \d{4} - four digits.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文