2

I have some string which I already mentioned in previous question

s = "[1,12:12] call basic_while1() Error Code: 1046. No database selected"

s1="[1,12:12] call add() Asdfjgg Error Code: 1046. No database aa selected"

s2= "[1,12:12] call add()"

s3= "[1,12:12]"

s4="Error Code: 1046. No database selected"

s5="END: Error Code: 2134. database doen't exist"

regex=(?:^\[(\d+),(\s?[0-9:]+)\](?:\s+(.+?)\s?)?(?:Error Code:.*)?$)|(?:()()()(Error Code:.*$))

using that regex I get output like

['1', '12:12', 'call basic_while1()']
['1', '12:12', 'call add() Asdfjgg']
['1', '12:12', 'call add()']
['1', '12:12']
['', '', '', 'Error Code: 1046. No database selected']

now I want to make change in that regex only and get out put like

['', '', '','Error Code: 1046. No database selected']
['', '', '','Error Code: 1046. No database selected']
['', '', '']
['', '']
['Error Code: 1046. No database selected']

I modified this to

(?:^\?:[(\d+),(\s?[0-9:]+)\](?:\s+(.+?)\s?$)?(Error Code:.*))|(?:()()()(Error Code:.*$))  

but it didn't work is it possible to get output like this using that Regex?

7
  • Yes, what have you tried to get the output you want? Commented Dec 21, 2018 at 7:17
  • wait , let me add it into a question Commented Dec 21, 2018 at 7:18
  • You can't have newlines in string literals unless you use triple quotes. Commented Dec 21, 2018 at 7:21
  • got it, and I recommend you to watch my last question @Barmar Commented Dec 21, 2018 at 7:23
  • Your question should be standalone. While linking toa previous question is fine as a reference, it should not be required to fully understand what you're asking. Commented Dec 21, 2018 at 7:26

2 Answers 2

1

Try with

regex='Error Code: \d+.*'
match = re.search(regex, s)
if match:
    print(match.group(0))
    # Output:
    # 'Error Code: 1046. No database selected'
Sign up to request clarification or add additional context in comments.

2 Comments

it works , but if scenario like s2 and s3 , it gives me a error , I want to make only one regex for it
@ThePjot this sol is good , but I want to change in this regex=(?:^[(\d+),(\s?[0-9:]+)](?:\s+(.+?)\s?)?(?:Error Code:.*)?$)|(?:()()()(Error Code:.*$)) and want this kind of output .I don't want to make new regex , thanks
1

You could use

^
(?:\[(?P<d1>[\d,]+):(?P<d2>[\d,]+)\]\ ?)?
(?:(?P<code>(?:(?!Error\ Code).)*))?
(?P<error>Error\ Code:.+)?

in verbose mode, see a demo on regex101.com.


Broken down this says:

^                              # start of the line
(?:                            # non-capturing group
    \[                         # [
    (?P<d1>[\d,]+):            # digits and commas -> group "d1", followed by :
    (?P<d2>[\d,]+)             # group "d2
    \]\ ?                      # make the space optional
)?                             # make the whole group optional
(?:
    (?P<code>
        (?:(?!Error\ Code).)*) # everything until "Error Code"
     )?
(?P<error>Error\ Code:.+)?     # Error Code and anything that follows


In Python this could be:

import re
rx = re.compile(r"""...above expression...""", re.M | re.X)
for m in rx.finditer(string):
    print(m.group('error')) # or any other group

See a demo on ideone.com for the whole snippet.

1 Comment

this sol is good , but I want to change in this regex=(?:^[(\d+),(\s?[0-9:]+)](?:\s+(.+?)\s?)?(?:Error Code:.*)?$)|(?:()()()(Error Code:.*$)) and want this kind of output .I don't want to make new regex , thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.