-1

I have a string like

string = "Status\t\t: PASS"

I want to fetch only PASS from this string and I am using this regex.

value = re.findall("Status" + r'(.*)', string)

But it returns me this

"           : PASS"

I want the regex to ignore all extra characters spaces tabs etc. Please let me know how can I do this.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
sam
  • 203
  • 1
  • 3
  • 15

3 Answers3

3

Method : Using regex() + string.punctuation This method also used regular expressions, but string function of getting all the punctuations is used to ignore all the punctuation marks and get the filtered result string.

# Python3 code to demonstrate 
# to extract words from string 
# using regex() + string.punctuation 
import re 
import string 

# initializing string 
test_string = "Geeksforgeeks, is best @# Computer Science Portal.!!!"

# printing original string 
print ("The original string is : " + test_string) 

# using regex() + string.punctuation 
# to extract words from string 
res = re.sub('['+string.punctuation+']', '', test_string).split() 

# printing result 
print ("The list of words is : " + str(res)) 

Output:

The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The list of words is : [‘Geeksforgeeks’, ‘is’, ‘best’, ‘Computer’, ‘Science’, ‘Portal’] 
  • I want to return the string after Status. I dont want to get all words in a string – sam Nov 25 '20 at 06:41
2

Would you please try the following:

import re
string = "Status\t\t: PASS"
m = re.search(r'Status\s*:\s*(.*)', string)
print(m.group(1))

Output:

PASS

Explanation of the regex Status\s*:\s*(.*):

  • Status\s* matches substring "Status" and following blank characters as may as possible if any.
  • :\s* matches a character ":" and following blank characters as many as possible if any.
  • (.*) matches the remaining substring and capture group 1 is assigned to it.
tshiono
  • 21,248
  • 2
  • 14
  • 22
1

Try this: regex-demo

Python-source:

import re

input1 = "Status\t\t: PASS"
input2 = "Status\t\t: PASS hello"
input3 = "Status\t\t: FAIL hello world"
regex=re.compile('status\s*:\s*(\w+)',flags=re.IGNORECASE)

print(f'result of input1: \n {regex.findall(input1)}')
print(f'result of input2: \n {regex.findall(input2)}')
print(f'result of input3: \n {regex.findall(input3)}')

Out-put:

result of input1: 
 ['PASS']
result of input2: 
 ['PASS']
result of input3: 
 ['FAIL']
Heo
  • 266
  • 2
  • 10