-3

I am programming in Python 3.4. I am reading lines in different files and when I get to the line containing a specific string I want extract only the path string from that line. Example:

line in file is:

TEST_CASE_CHECK = require("TestCases/AOL/TU_Extract_Log_Details")

I search for: require("TestCases

I want extract only: TestCases/AOL/TU_Extract_Log_Details

I know this can be done with regex but I could not figure out how to do it.

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
marius alex
  • 1
  • 1
  • 1

3 Answers3

0

I don't know what the rest of your lines look like but this should work

import re

s = 'TEST_CASE_CHECK = require("TestCases/AOL/TU_Extract_Log_Details")'

mat = re.search(r'(TestCases.*)"',s)
if mat:
    print(mat.group(1))

TestCases/AOL/TU_Extract_Log_Details
SirParselot
  • 2,640
  • 2
  • 20
  • 31
0

Is it what you are looking for?

import re
p = re.compile(ur'require\("(TestCases[^"]*)')
test_str = u"TEST_CASE_CHECK = require(\"TestCases/AOL/TU_Extract_Log_Details\")"

re.findall(p, test_str)

https://regex101.com/r/zJ0sM3/1

Doro
  • 755
  • 8
  • 25
0
import re

line = 'TEST_CASE_CHECK = require("TestCases/AOL/TU_Extract_Log_Details")'

result = re.match(r'.*require.+"([^"]*)".*', line)

print result.groups()[0] # prints "TestCases/AOL/TU_Extract_Log_Details"
Robert
  • 33,429
  • 8
  • 90
  • 94