2

How to make a regex for this:

[1,31:14:18] SELECT * FROM T.roll1

Here what i tried r'\[[^)] \s' but it is not able to give me a output like:

(1, 31:14:18 ,SELECT * FROM T.roll1)

How to get this output?

3
  • 1
    You can use regex same as regex101.com/r/RdPxxw/1 data = re.search(pattern, text, re.IGNORECASE) and after use data.group(1-3) Commented Dec 11, 2018 at 12:15
  • What do you need as output? Python2 or 3? Commented Dec 11, 2018 at 13:55
  • while ppl here start answering the "question" to get a karma, i would tell that the question is unclear! (specially i can't understand, why here mentioned python if OP asked the question about how to make regex?) Commented Dec 11, 2018 at 14:04

2 Answers 2

1

You can use something like:

import re
s = "[1,31:14:18] SELECT * FROM T.roll1"
r = [x.strip("[") for x in re.split(",|] ", s)]
print(r)

['1', '31:14:18', 'SELECT * FROM T.roll1']

Python Demo

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

Comments

0

You will need to do three different regex :

import re

test_str = "[1,31:14:18] SELECT * FROM T.roll1"

first_regex = r"\d+"
second_regex = r",(.*)]"
third_regex = r"] (.*)"

first_matches = re.findall(first_regex, test_str, re.MULTILINE)
second_matches = re.findall(second_regex, test_str, re.MULTILINE)
third_matches =  re.findall(third_regex, test_str, re.MULTILINE)

tuple_matches = first_matches[0], second_matches[0], third_matches[0]

print(tuple_matches)

# Output
# ('1', '31:14:18', 'SELECT * FROM T.roll1')

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.