-2

I have a below file, How to extract the row which is having 5.6 version number. I need to find the regex pattern

A   KB           1024               MYsql root
B   GB            1                 ELK-6
C   KB           1024               Mysql5.6
E   KB           1024               ELK
F   GB           1                  Mysql5.6

Expected Out

C   KB           1024               Mysql5.6
F   GB           1                  Mysql5.6

My Code in pandas

import re
infile = r'C:\user\Desktop\m.txt'
df_list = []
with open(infile) as f:
    for line in f:
        # remove whitespace at the start and the newline at the end
        line = line.strip()
        # split each column on whitespace
        columns = re.split('\s+', line, maxsplit=4)
        df_list.append(columns)
df = pd.DataFrame(df_list)
df[df[3].str.contains("5.6")]

I need to do with regex with Python not with Pandas dataframe. i didnt tag pandas

  • 2
    Possible duplicate of [How to filter rows containing a string pattern from a Pandas dataframe](https://stackoverflow.com/questions/27975069/how-to-filter-rows-containing-a-string-pattern-from-a-pandas-dataframe) – Chris Sep 17 '19 at 05:23
  • not with pandas, with regex, with pandas i have wrote the code –  Sep 17 '19 at 05:39
  • Is your input a long string then? – Chris Sep 17 '19 at 05:41
  • No, its not long string –  Sep 17 '19 at 05:44
  • `pandas.Series.str.contains` already assumes its `pat` as _regex_, which means that your code in pandas is already utilizing regex. Try adding row with `Mysql root == Mysql5A6` and `str.contains("5.6")` will still catch it since `.` is automatically understood as regex. – Chris Sep 17 '19 at 05:48
  • I need for regex format. not with pandas –  Sep 17 '19 at 06:06

2 Answers2

0

This regex will match all string with 5.6 version.

^.*5\.6$
xrq0
  • 119
  • 13
0

I am not, unfortunately, up on pandas, but:

re.search(r'5\.6', s)

will return a match object, which evaluates to True, if the string '5.6' exists anywhere in string s or None, which evaluates to False, if it doesn't.

Booboo
  • 38,656
  • 3
  • 37
  • 60