1

I am trying to match the following line in python however this line is not working.

Example text is:

usr/local/java/latest/bin/java-Djava.util.logging.config.file=/usr/local/tomcat/foobarcode/conf/

Example python:

re.match(r"/usr/local/java/latest/bin/java[-]Djava[.]util[.]logging[.]config[.]file[=]/usr/local/tomcat/(\.*)/conf/\.*", pidInfo)

Any help that can be provided will be greatly appreciated. Thanks!

2
  • \.* would match a literal . 0 or more times. Commented Dec 14, 2012 at 22:05
  • @Ashwini Chaudhary, your edit breaks the Python syntax. If the long lines are a problem for you, use the `` line continuation char, but don't break the syntax. Commented Dec 14, 2012 at 22:14

2 Answers 2

1

The problem is that you escape the .. That makes it a literal period. You want to leave it unescaped so that it becomes the wildcard:

re.match(r"/usr/local/java/latest/bin/java-Djava[.]util[.]logging[.]config[.]file=/usr/local/tomcat/(.*)/conf/.*", pidInfo)

Also, your input example does not contain the leading /, but that might be a copy-paste mistake. (And note that there is no need to escape = and -).

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

2 Comments

D0h - the issue is it appears off-screen the area you're referring to!
@JonClements I prefer the scrollbar to the random line-breaks in how the question is rendered ;)
1

Here's the fixed version of what you're trying to do:

>>> s = '/usr/local/java/latest/bin/java-Djava.util.logging.config.file=/usr/local/tomcat/foobarcode/conf/'
>>> re.match(r'/usr/local/java/latest/bin/java-Djava\.util\.logging\.config\.file=/usr/local/tomcat/(.*)/conf/.*', s)
<_sre.SRE_Match object at 0x1004a0648>

Issues:

  1. You do not need to escape the . as a wildcard
  2. You do need to (elegantly) escape \. when used as dots in filename ([.] works just fine, just less elegant IMO)
  3. Missing root / in filename
  4. - and = chars do not need escaping

3 Comments

My version, I like it better, but yeah [.] works as well :)
Yeah, but you say it like \. is the way to go, although it is really nothing but a matter of taste. I find the character class escape technique much more readable in many cases.
I tried running the regex you created and it still won't match unless I change the "-" to a ".+" Other than that it seems to work flawlessly.

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.