1

I have a string as

str='[dbo].[AGENTS]'

I want to retreive only first bracket value using regular expression how can I do that

output : dbo

shee8
  • 139
  • 10
  • 2
    no need to use regex. just use this: https://stackoverflow.com/questions/4894069/regular-expression-to-return-text-between-parenthesis replace the parentheses with brackets – Muhammad Nizami Dec 16 '20 at 10:30
  • If you decide to do regex, than, possibly [`[^][]+`](https://regex101.com/r/dU42kT/2) – JvdV Dec 16 '20 at 10:59

1 Answers1

1

You could do that using groups:

test_string = '[dbo].[AGENTS]'
pattern = "\[(\w+)\].\[(\w+)\]"
match = re.match(pattern, test_string)
print(match.group(1))

Output

dbo
andy meissner
  • 1,202
  • 5
  • 15