0

I have the following text:

"

In the Matter of

XYZ-ABCD

Respondent.

"

Stashed away at some part of a pdf file. I am only interested in capturing the XYZ-ABCD part but apparently the regex I am using in python is not capturing the pattern correctly.

The piece of text I am interested in capturing can appear anywhere within the PDF and I am using the following pattern:

 pat = "^\n+In the Matter of\n+(\s+\w+\s*)\n+ 
 (Respondent\.|Respondents\.)\s+$" 

This is the regex code I am using to capture

 str = re.match(pat,input_str)

Obviously, I have included the \n to take care of the multiple lines, However, I don't seem to be getting any matches and don't seem to see what I am missing in my pattern that has not included. This also includes partial matches which I don't seem to be getting.

4
  • Have you tried giving the positional argument re.MULTILINE at the end of your regex call? For that matter, can you show us the actual regex call you're currently trying to use (that is, the code involved in this question)? Commented Jul 22, 2019 at 18:26
  • 1
    is this what you're looking for ?, your \s+ should be \s* i.e (\s*\w+\s*) Commented Jul 22, 2019 at 18:27
  • If you are on Windows you might need \r\n for new lines. Can you break down your regex bit by bit until it does give a match? Then you should be able to tell us the exact regex character that is giving issues (or maybe the answer will become obvious to you). Commented Jul 22, 2019 at 18:59
  • There is no dash - in a word \w try [-\w] and see if that helps. Commented Jul 22, 2019 at 20:46

1 Answer 1

1

You could use

^\s+In the Matter of\s+(\S+)\s+Respondents?

See a demo on regex101.com (mind the multiline flag).


Some issues with your original expression:

\n != \s       # \s includes \n but also other whitespace characters
\w = [A-Z0-9_] # but you wanted to match "-" as well which is not part of \w

Additionally, you had likely neither the multiline nor the verbose flag on but your code snippet looked like you would have needed to.

Sign up to request clarification or add additional context in comments.

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.