How to show full results, rather than matched text from regex searches in python

Ozymandias

I am creating a script that searches for a file based on a keyword, my output should be the whole observation, rather than just the matched text, but I'm finding .group doesn't work on this.

import re 
import os 
 
pers_info = pd.read_csv(r".....StateWorkforceMailingList_2-7-19a.csv",encoding='utf-8')

Pers_info['State'] = Texas, Florida etc... 

 files=os.listdir(r"....\State Files")
 
Files = list of WORKFORCE_2017_ALABAMA_FILE.xlsx,...,n

matches=re.findall(pers_info.State[4], files.replace("_", " "),re.I)
print(match) 

My intended output is WORKFORCE_2017_ALABAMA_FILE.xlsx Instead I get 'Alabama'

Should I try a boolean mask ?

Ryszard Czech

Use

>>> import pandas as pd
>>> Pers_info = pd.DataFrame({'State':['Texas', 'Alabama', 'Florida']})
>>> Files = ['WORKFORCE_2017_ALABAMA_FILE.xlsx', 'WORKFORCE_2017_FILE.xlsx']
>>> pattern = re.compile(rf'(?<![^\W_])(?:{"|".join(Pers_info["State"].to_list())})(?![^\W_])', re.I)
>>> list(filter(pattern.search, Files))
['WORKFORCE_2017_ALABAMA_FILE.xlsx']

See regex proof.

EXPLANATION

--------------------------------------------------------------------------------
  (?<!                     look behind to see if there is not:
--------------------------------------------------------------------------------
    [^\W_]                   any character except: non-word
                             characters (all but a-z, A-Z, 0-9, _),
                             '_'
--------------------------------------------------------------------------------
  )                        end of look-behind
--------------------------------------------------------------------------------
  (?:                      group, but do not capture:
--------------------------------------------------------------------------------
    Texas                    'Texas'
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
    Alabama                  'Alabama'
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
    Florida                  'Florida'
--------------------------------------------------------------------------------
  )                        end of grouping
--------------------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
    [^\W_]                   any character except: non-word
                             characters (all but a-z, A-Z, 0-9, _),
                             '_'
--------------------------------------------------------------------------------
  )                        end of look-ahead

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How can I make Xamarin show the hamburger icon rather than the back button?

분류에서Dev

Pasting image into Outlook results in an attachment, rather than inline with content

분류에서Dev

Is it possible to show data by row rather than per column?

분류에서Dev

How to link python to the manually compiled OpenSSL rather than the system's one

분류에서Dev

How to replace a matched group value with Regex

분류에서Dev

MySQL Full Text Search Returning 0 Results

분류에서Dev

MySQL full text search matching similar results

분류에서Dev

How do you get the ASP.NET MVC action of the page (i.e. one from URL) rather than current action?

분류에서Dev

Difference in select query result on MyISAM Vs InnoDB MySQL engines(especially for FULL TEXT SEARCHES)

분류에서Dev

TextView doesn't show full text

분류에서Dev

How to use host names rather than ip addresses on home network?

분류에서Dev

how to use Django Hitcount in a function based view rather than a class?

분류에서Dev

vim - how to treat three quotations in a row as a comment rather than a string

분류에서Dev

How to pass an array to a function as an actual parameter rather than a global variable

분류에서Dev

How to build openssl with clang(rather than gcc) on a FreeBSD machine?

분류에서Dev

How to save multiple files to database rather than disk

분류에서Dev

extract and assign matched regex pattern from string in Perl to a variable

분류에서Dev

Select box text equal to rather than Select Box text contains - jQuery

분류에서Dev

how to show figure from linux terminal in python?

분류에서Dev

How to get index of regex match of only the matched and included part?

분류에서Dev

how to get SELECT * FROM TABLE results into more than one variable in c#

분류에서Dev

Python 3.4 : match from csv and return new csv with matched values

분류에서Dev

how to write into a text file from python 3.3?

분류에서Dev

why is python argparse giving the help for imported module rather than current module?

분류에서Dev

How to show results in a list Grails gsp

분류에서Dev

Using python3.0 and above to enter a string into a text box and getting the results from a website

분류에서Dev

How can I implement Deleting files to the Recycle bin rather than Permanently deleting?

분류에서Dev

Express 4: How to upload file to memory (e.g. as a UTF-8 string) rather than disk?

분류에서Dev

How can I get PyCharm to use the Vagrant Directories rather than my development machines?

Related 관련 기사

  1. 1

    How can I make Xamarin show the hamburger icon rather than the back button?

  2. 2

    Pasting image into Outlook results in an attachment, rather than inline with content

  3. 3

    Is it possible to show data by row rather than per column?

  4. 4

    How to link python to the manually compiled OpenSSL rather than the system's one

  5. 5

    How to replace a matched group value with Regex

  6. 6

    MySQL Full Text Search Returning 0 Results

  7. 7

    MySQL full text search matching similar results

  8. 8

    How do you get the ASP.NET MVC action of the page (i.e. one from URL) rather than current action?

  9. 9

    Difference in select query result on MyISAM Vs InnoDB MySQL engines(especially for FULL TEXT SEARCHES)

  10. 10

    TextView doesn't show full text

  11. 11

    How to use host names rather than ip addresses on home network?

  12. 12

    how to use Django Hitcount in a function based view rather than a class?

  13. 13

    vim - how to treat three quotations in a row as a comment rather than a string

  14. 14

    How to pass an array to a function as an actual parameter rather than a global variable

  15. 15

    How to build openssl with clang(rather than gcc) on a FreeBSD machine?

  16. 16

    How to save multiple files to database rather than disk

  17. 17

    extract and assign matched regex pattern from string in Perl to a variable

  18. 18

    Select box text equal to rather than Select Box text contains - jQuery

  19. 19

    how to show figure from linux terminal in python?

  20. 20

    How to get index of regex match of only the matched and included part?

  21. 21

    how to get SELECT * FROM TABLE results into more than one variable in c#

  22. 22

    Python 3.4 : match from csv and return new csv with matched values

  23. 23

    how to write into a text file from python 3.3?

  24. 24

    why is python argparse giving the help for imported module rather than current module?

  25. 25

    How to show results in a list Grails gsp

  26. 26

    Using python3.0 and above to enter a string into a text box and getting the results from a website

  27. 27

    How can I implement Deleting files to the Recycle bin rather than Permanently deleting?

  28. 28

    Express 4: How to upload file to memory (e.g. as a UTF-8 string) rather than disk?

  29. 29

    How can I get PyCharm to use the Vagrant Directories rather than my development machines?

뜨겁다태그

보관