0

My mind is getting a little numb with regex. I need to come up with a regular expression to match any of the following scenarios, but so far I have only come up with the following expression that isn't quite working:

(package)(\.)(path)(\s+)(=)( )(').*?..*?..*?..*?(.).*?(')

I need to build two different expressions.

Expression #1 should match any of the following strings.

package.path = 'any string or path here'
package.path = "any string or path here"
package.path='any string or path here'
package.path="any string or path here"

Expression #2. should match any of the following strings.

package.path = package.path .. 'any string or path here'
package.path = package.path .. "any string or path here"
package.path = package.path..'any string or path here'
package.path = package.path.."any string or path here"
package.path=package.path .. 'any string or path here'
package.path=package.path .. "any string or path here"

I would appreciate any help from a Regex Guru out there.

Thanks.

11
  • for starters, . is a special char in regex, you'll want to escape it with \. Commented Apr 9, 2014 at 22:42
  • Could you provide a bit more info on what might constitute "any string or path"? Do the paths contain forward slashes or back slashes? Do they contain drive letters? Do the strings only contain numbers and letters, or could they contain other characters or even whitespace? In the second part, do you need to match a literal .. or does that symbolize something else? Commented Apr 9, 2014 at 22:46
  • ^ or put it in a character class (I tend to think they are easier on the eyes than escaping): [.]. Commented Apr 9, 2014 at 22:46
  • \s+ requires space. Sounds like you want space to be optional. That should be \s*. Commented Apr 9, 2014 at 22:47
  • \s? (which many folks are suggesting) is only correct if the most you could have is a single space character. Is that the case, or could there be runs of more than one space? Commented Apr 9, 2014 at 22:48

3 Answers 3

1

For starters, . is a special char in regex, you'll want to escape it with \.

then, + means one or more. Your spaces are present 0 or 1 times, which is indicated by ?

then, character classes are indicated using [], so your quotes can be classified as ['"]

finally, we can use \1 to recall the matched quote

With that in mind, your first expression is

package\.path\s?=\s?(['"]).*\1

and the second is

package\.path\s?=\s?package\.path\s?\.\.\s?(['"]).*\1
Sign up to request clarification or add additional context in comments.

Comments

1

You can use package\.path\s?=\s?(package\.path)?(\s\.\.\s)?('|").*?('|") regex:

import re

data = """package.path = 'any string or path here'
package.path = "any string or path here"
package.path='any string or path here'
package.path="any string or path here"
package.path = package.path .. 'any string or path here'
package.path = package.path .. "any string or path here"
package.path=package.path .. 'any string or path here'
package.path=package.path .. "any string or path here"
"""


pattern = re.compile(r"""package\.path\s?=\s?(package\.path)?(\s\.\.\s)?('|").*?('|")""")
for line in data.splitlines(False):
    match = pattern.match(line)
    print match.group(0) if match else "No match"

Comments

1

Expression #1: package.path\s?=\s?['"][\w\s]+['"]
Expression #2: package.path\s?=\s?package.path\s..\s['"][\w\s]+['"]

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.