Python Forum
regex to extract only yy or yyyy
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
regex to extract only yy or yyyy
#2
You should post what you tried(like in working code),and also test example with input and wanted output
I can me a mess of text that you work with,or it can be more structured.
(Jul-11-2018, 11:29 AM)metalray Wrote: For the second pattern, I need to make sure that the year stands alone and has no more digits before or after.
>>> import re
>>> 
>>> s = '1980 100 18000 2000 112 2018'
>>> re.findall(r'(?<!\d)\d{4}(?!\d)', s)
['1980', '2000', '2018']

# only 2
>>> s = '19 100 18000 20 1234 1 55'
>>> re.findall(r'(?<!\d)\d{2}(?!\d)', s)
['19', '20', '55']
Reply


Messages In This Thread
regex to extract only yy or yyyy - by metalray - Jul-11-2018, 11:29 AM
RE: regex to extract only yy or yyyy - by snippsat - Jul-11-2018, 12:40 PM
RE: regex to extract only yy or yyyy - by volcano63 - Jul-11-2018, 07:57 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Need to sum up HHMMS AM/PM DD/MM/YYYY and a HH:MM:SS. tester_V 3 1,311 Jan-04-2023, 09:19 PM
Last Post: tester_V
  [SOLVED] Alternative to regex to extract date from whole timestamp? Winfried 6 1,987 Nov-16-2022, 01:49 PM
Last Post: carecavoador
  regex pattern to extract relevant sentences Bubly 2 1,954 Jul-06-2021, 04:17 PM
Last Post: Bubly
  Regex to extract IPs between () not working mrapple2020 5 3,529 Apr-12-2019, 08:03 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020