Daedalus
Daedalus

Reputation: 305

Regex - Why won't this regex work in Python?

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

Upvotes: 1

Views: 59

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626845

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.

Upvotes: 1

Related Questions