Reputation:
I am trying to extract a pattern from a string using the regex -
\"([a-zA-Z0-9/-_\"]+)\""
command = '"custom-ts-name": "asdf-somenum"'
search_string = "\"custom-ts-name\": \"([a-zA-Z0-9/-_\"]+)\""
pattern = re.compile(search_string)
ts_name = pattern.findall(command)[0]
It works most of the time, except when the result string, asdf-somenum
contains a hypen in it.
I have added hyphen between square brackets inside the regex - \"([a-zA-Z0-9/-_\"]+)\""
, to address this but not sure, why it crashes still?
Upvotes: 0
Views: 119
Reputation: 36110
-
inside character sets has the special meaning of from-to. For example, the a-z
you have used means all lowcase letters. You can put the dash in either first/last position or escape it:
\"([a-zA-Z0-9/_\"-]+)\""
Upvotes: 2