-2

I'm trying to match any word with a open parenthesis: '(', and I came up with this regex which so far matches words with an open parenthesis but stops when another special character, like a dot or a close parenthesis appears. I'd like to match the full word until a space comes up.

 re.compile(r'[a-zA-Z]*\([a-zA-Z]*', flags=re.IGNORECASE)
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
S420L
  • 117
  • 10
  • 2
    *"I want to use regex but don't want to learn to use regex"* isn't a great intro. – jonrsharpe Dec 01 '19 at 21:26
  • Does this answer your question? [Regular expression to extract text between square brackets](https://stackoverflow.com/questions/2403122/regular-expression-to-extract-text-between-square-brackets) – oo00oo00oo00 Dec 01 '19 at 21:28
  • 3
    This tool has been invaluable to me for these kinds of questions, just don't forget to change the language to python: https://regex101.com/ – Slowat_Kela Dec 01 '19 at 21:29

1 Answers1

0

Would something like that do the job? r"\(([a-zA-Z]*) "

You do not need to specify what is before or after the pattern you want to match, as soon as you do not use the ^and $symbols that stands for the start and the end of the string.

So taking your example, the first group you use [a-zA-Z]*is useless if you don't have any requirement on what is before your open parenthesis. Furthermore you do another mistake because in your regex you do not put a space after the second [a-zA-Z]* group. Because you want to match every word that is between an open parenthesis and a space, you need to put the space in the regex string.

EDIT: The solution to solve the problem is this ([^ ]*\([^ ]*) read through the comments on this to see discussion

Thombou
  • 367
  • 2
  • 15
  • Nah only matches things between parenthesis now: if it's ```sum(bytes)``` it gives you back ```bytes``` rather than the whole thing – S420L Dec 01 '19 at 21:55
  • Then I did not understand your request. You said _I'd like to match the full word until a space comes up_ so I understand that you want to match what is inside the parenthesis. Can you specify examples of strings and what should be returned by the regex? – Thombou Dec 01 '19 at 21:57
  • it should return anything that remotely resembles a word with an open parenthesis somewhere in it, here's a comma separated list of examples: `distinct(sale, count(distinct IDs), sum(bytes), avg(money)/6` – S420L Dec 01 '19 at 22:01
  • This one will capture all groups where you have a letter or more, an open parenthesis and then everything until a space appears: ```([a-zA-Z]*\([^ ]*)```. – Thombou Dec 01 '19 at 22:05
  • Thanks! This works, ended up using `([^ ]*\([^ ]*)` because it matches literally anything separated by space with a "(", glad I now have this in my arsenal – S420L Dec 01 '19 at 22:13