0

I tried to get some string between two keywords from a large text file with the following pattern searching each line by line and print it as well as store in another text file

'Event_WheelMonitorReleased' (253) 'Event_WheelMonitorPressed' (252) 'Event_WheelMonitorPressed' (252) 'Event_WheelMonitorPressed' (252)

Here I would like to extract only the strings inbetween EVENT()

Here I would say I need X_0_Gui_Menu_610_Menu_Status_System

I tried the following code

def get_navigated_pages():
    os.chdir('log_file')
    log_file = open('messages','r')
    data = log_file.read()
    navigated_pages = re.findall(r'EVENT(X(.*?)) ',data,re.DOTALL|re.MULTILINE)
    with open('navigated_page_file', 'w') as navigated_page_file:
         navigated_page_file.write(navigated_pages)

I expected the output in the text file to be something like this

X_0_Gui_Menu_650_Menu_Status_Version 
X_0_Gui_Menu_610_Menu_Status_System 
X_0_Gui_Menu_670_Menu_Status_Media

As mentioned above I would like to get the output only which is starting with X_0 and ignoring starting with other keywords

3
  • What was the output instead? Commented Nov 19, 2014 at 18:40
  • I just get ('Navigated pages: ', []) at the print statement and nothing at all in the text file Commented Nov 19, 2014 at 18:42
  • Not with the code you show here. You have a syntax error (missing a with before open) and multiple logic and spelling errors (navigated_page_file versus navigated_pages_file, giving a list object to write). In your comment you mention a print statement but show none in your code. You should show your actual code. Commented Nov 19, 2014 at 18:44

2 Answers 2

1

Try escaping your outermost parentheses pair.

navigated_pages = re.findall(r'EVENT\(X(.*?)\) ',data,re.DOTALL|re.MULTILINE)

This appears to make it match properly, at least for my little sample input:

>>> s = "EVENT(X_HELLO) ... EVENT(X_HOW_ARE_YOU_DOING_TODAY)... EVENT(this one shouldn't appear because it doesn't start with X)"
>>> re.findall(r"EVENT\(X(.*?)\)", s)
['_HELLO', '_HOW_ARE_YOU_DOING_TODAY']

If you want the starting X too, you should nudge the inner parentheses to the left by one. Don't worry, I'm pretty sure the *? will still have the proper precedence.

>>> re.findall(r"EVENT\((X.*?)\)", s)
['X_HELLO', 'X_HOW_ARE_YOU_DOING_TODAY']
Sign up to request clarification or add additional context in comments.

4 Comments

Hi, I forgot to add another info is that there are many other such EVENTS like EVENT(global_Z_Power_Popup) ..... with the following code it prints everything but I want only the ones which start with EVENT(X_ ...)
@VenkateshPadmanabhan, ok, and my code does only show the ones starting with X. Or are you seeing something different?
I tried the second variation which you posted as I wanted the X keyword too. But it posts all the strings irrespective of the starting keyword. Everything which is inside EVENT()
It prints something like this global_ImportActive_Popup', 'global_ListDelete_Confirm_Popup', 'global_OperationFailed_Popup', 'global_W_PlayPressInfo_Popup', 'global_WatchdogWelcomeNavigation', 'global_X_Locked_Cam_Info', 'global_Y_LockedDuringRecPrerec_Info', 'global_Z_Power_Popup', 'done', 'post', 'start', 'X_0_Gui_Menu_000_Menu_root', 'global_ExportActive_Popup', 'global_FileOverwrite_Confirm_Popup', 'global_Global_Reactions', 'global_ImportActive_Popup', 'global_ListDelete_Confirm_Popup', 'global_OperationFailed_Popup', 'global_W_PlayPressInfo_Popup', 'global_WatchdogWelcomeNavigation'
1

might get away with using split:

s = "Jan 01 08:11:13 AMIRA-134500021 user.notice gui-monitor[770]: ACTION:401b0836:8:EVENT(X_0_Gui_Menu_610_Menu_Status_System) 'Event_WheelMonitorReleased' (253)"
print(s.split("EVENT(")[1].rsplit(") ",1)[0])
X_0_Gui_Menu_610_Menu_Status_System

with open('message','r') as log_file:
    for line in log_file:
        print(line.split("EVENT(")[1].rsplit(") ",1)[0])

X_0_Gui_Menu_610_Menu_Status_System
X_0_Gui_Menu_610_Menu_Status_System
global_ExportActive_Popup
global_FileOverwrite_Confirm_Popup
global_Global_Reactions

To get only X_ lines:

with open('message','r') as log_file:
    for line in log_file:
        chk = line.split("EVENT(")[1].rsplit(") ",1)[0]
        if chk.startswith("X_"):
            print(chk)
X_0_Gui_Menu_610_Menu_Status_System
X_0_Gui_Menu_610_Menu_Status_System

If you are confident X_ only appears in the lines you want:

 for line in log_file:
    if "X_" in line:
        chk = line.split("EVENT(")[1].rsplit(") ",1)[0]
        print(chk)

1 Comment

But I would like to store only the string which start with X_ I dont want other string which starts with other keywords.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.