0

I want a regex expression to search logger.error("pbType",

and exclude the ones with {} for example:

logger.info("URL:\n\"{}\"\n",

this does not work for me - re.search('.*logger.*"[\w.:-_()\[\]]*"\s*,',line)

It returns me lines with {}. Please help Thanks

7
  • I dont understand the question. Do you want to remove the braces or do you want to ignore the strings having curly braces? Commented Jul 27, 2013 at 1:32
  • There are many such log statements similar to what I posted above. I want to ignore the ones having curly braces. Commented Jul 27, 2013 at 1:36
  • What do you think your regex is searching for now? Commented Jul 27, 2013 at 1:37
  • Basically I need a way to nagate curly braces from my search. Thanks for the help Commented Jul 27, 2013 at 1:37
  • Jim, right now it returns both lines with curly braces and without them. Commented Jul 27, 2013 at 1:38

2 Answers 2

2

Let's see how your current regular expression is parsing the line in question:

.*|logger|      .*          |"|[\w.:-_()\[\]]*|"|\s*|,
  |      |                  | |               | |   |
  |logger|.info("URL:\n\"{}\|"|\n             |"|   |,

It's picking up the third quotation mark as the first one in the regular expression.

To fix, you want to be sure that the ".*"s don't grab more than you want them to.

[^"\n]*logger[^"\n]*"[\w.:-_()\[\]]*"\s*,

Also, there are a few other mistakes in your current regex:

[ :-_ ] includes all characters in the ascii range 58 to 95. if you want to include a minus sign in a character set, it must go first.

  [-\w.:_()\[\]]

It's good style to use raw strings for regular expressions, as you know that backslashes will be backslashes instead of triggering an escape sequence.

 re.search(r'...', line)

You want to make sure the "\s*, really gets the end of the string, there could be a \",{} at the end you don't catch , so match an end of line in your regex ...$

all together, these suggestions would make your line of code:

re.search(r'[^"\n]*logger[^"\n]*"[-\w.:-()\[\]]*"\s*,$', line)
Sign up to request clarification or add additional context in comments.

Comments

1

Just do a if. No need of regex -

for i in l1:
    if not("{}" in i):
        l2.append(i)

l2 is the required result considering l1 is the list of your strings.

2 Comments

Thanks for the reply svineet . Can you also help me with a regex? Thanks
not ("{}" in i) can be simplified to '{}' not in i. Furthermore, you can replace your whole loop with l2 = [item for item in l1 if '{}' not in item].

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.