Python Forum
Creating new list based on exact regex match in original list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating new list based on exact regex match in original list
#2
re.match returns a MatchObject or None. Your code is taking the "string" attribute of the MatchObject and adding that to the list. The string attribute is the string passed to match. Take a look at MatchObject and see how it can provide the string you really want.

This may do what you want:
secondList = [i.group(0) for i in firstList if regex.match(i)]
Reply


Messages In This Thread
RE: Creating new list based on exact regex match in original list - by deanhystad - Mar-08-2020, 09:30 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  unable to remove all elements from list based on a condition sg_python 3 465 Jan-27-2024, 04:03 PM
Last Post: deanhystad
  Facing issue in python regex newline match Shr 6 1,332 Oct-25-2023, 09:42 AM
Last Post: Shr
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,188 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Move Files based on partial Match mohamedsalih12 2 834 Sep-20-2023, 07:38 PM
Last Post: snippsat
Question in this code, I input Key_word, it can not find although all data was exact Help me! duchien04x4 3 1,058 Aug-31-2023, 05:36 PM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 1,558 May-01-2023, 09:06 PM
Last Post: deanhystad
  Failing regex, space before and after the "match" tester_V 6 1,201 Mar-06-2023, 03:03 PM
Last Post: deanhystad
  List all possibilities of a nested-list by flattened lists sparkt 1 929 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Regex pattern match WJSwan 2 1,283 Feb-07-2023, 04:52 AM
Last Post: WJSwan
  Split pdf in pypdf based upon file regex standenman 1 2,105 Feb-03-2023, 12:01 PM
Last Post: SpongeB0B

Forum Jump:

User Panel Messages

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