1

I have this expression

:([^"]*) \(([^"]*)\)

and this text

:chkpf_uid ("{4astr-hn389-918ks}")

:"#cert" ("false")

Im trying to match it so that on the first sentence ill get these groups:

  1. chkpf_uid
  2. {4astr-hn389-918ks}

and on the second, ill get these:

  1. #cert
  2. false

I want to avoid getting the quotes.

I can't seem to understand why the expression I use won't match these, especially if I switch the [^"]* to a (.*).

with ([^"]*): wont match

with (.*): does match, but with quotes

This is using the re module in python 2.7

4
  • 1
    You're regex won't match the quotes. Since there is nothing that can consume them, the regex fails. Commented Aug 1, 2016 at 10:49
  • You cannot make the regex jump from location to location. Negated character classes just do not act like this. A regex matches a string sequentially, and if there are quotes, you still need to match them. Match what you do not need, and match and capture what you need. :"?([^"]*)"? \("?([^"]*)"?\) Commented Aug 1, 2016 at 10:50
  • Every single character should have its own corresponding pattern: :"?([^"]*)"? \("{?(.*?)}?"\) Commented Aug 1, 2016 at 10:51
  • [^"]* matches everything except ". So either it matches the whole text or it reaches a position where there's a ", you then ask the regex to match a space , which always fails because either there is nothing left or there is a ". Commented Aug 1, 2016 at 11:23

1 Answer 1

1

Sidenote: your input may require a specific parser to handle, especially if it may have escape sequences.

Answering the question itself, remember that a regex is processed from left to right sequentially, and the string is processed the same here. A match is returned if the pattern matches a portion/whole string (depending on the method used).

If there are quotation marks in the string, and your pattern does not let match those quotes, the match will be failed, no match will be returned.

A possible solution can be adding the quotes as otpional subpatterns:

:"?([^"]*)"? \("?([^"]*)"?\)
 ^^       ^^   ^^       ^^

See the regex demo

The parts you need are captured into groups, and the quotes, present or not, are just matched, left out of your re.findall reach.

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.