2

I have the following string in python:

text = "vagrant  11450  4344  0 Feb22 pts/2    00:00:28 python run.py abc"

I want to capture the text after time field that is "python run.py abc"

I am using the following regex but it is not working

 [\d:]+ (.)*
2
  • 2
    Use r'\d+:\d+\s+(.*)' Commented Feb 22, 2018 at 20:02
  • 1
    @WiktorStribiżew Its perfectly working. Thanks a lot. Commented Feb 22, 2018 at 20:04

4 Answers 4

3

You may use

\d+:\d+\s+(.*)

See the regex demo.

Details

  • \d+ - 1 or more digits
  • : - a colon
  • \d+ - 1 or more digits
  • \s+ - 1 or more whitespace chars
  • (.*) - Group 1 (the value you need to access using .group(1)): any 0+ chars other than line break chars, as many as possible (all the rest of the line).

See the Python demo:

import re
text = "vagrant  11450  4344  0 Feb22 pts/2    00:00:28 python run.py abc"
m = re.search(r'\d+:\d+\s+(.*)', text)
if m:
    print(m.group(1)) # => python run.py abc
Sign up to request clarification or add additional context in comments.

Comments

1

With re.search() function:

import re

text = "vagrant  11450  4344  0 Feb22 pts/2    00:00:28 python run.py abc"
result = re.search(r'(?<=(\d{2}:){2}\d{2} ).*', text).group()

print(result)

The output:

python run.py abc

Comments

1

Without RE:

text = "vagrant  11450  4344  0 Feb22 pts/2    00:00:28 python run.py abc"
text=text.split(":")[-1][3:]

Output:

python run.py abc

Comments

1

You can use re.split and regex :\d{2}:\d{2}\s+.

text = 'vagrant  11450  4344  0 Feb22 pts/2    00:00:28 python run.py abc'
str = re.split(r':\d{2}:\d{2}\s+', text)[1]

Output: python run.py abc

Code demo

Comments

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.