Python Forum
Regular expression: match pattern at the end only - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Regular expression: match pattern at the end only (/thread-22804.html)



Regular expression: match pattern at the end only - Pavel_47 - Nov-27-2019

Hello,

Here are two strings:

Control Engineering – MATLAB Exercises (Advanced Textbooks in Control and Signal Processing)
Control Engineering – (AAAA) MATLAB Exercises (Advanced Textbooks in Control and Signal Processing)

1st string has one part in parentheses (at the end), 2nd string has two parts in parentheses: in the middle and at the end.

How math only trailing part in 2nd string using regex?
The pattern ' \(.+\)' matches like this:
Control Engineering – (AAAA) MATLAB Exercises (Advanced Textbooks in Control and Signal Processing)
Thanks.


RE: Regular expression: match pattern at the end only - Gribouillis - Nov-27-2019

Try this perhaps r"\([^()]*\)$"


RE: Regular expression: match pattern at the end only - Pavel_47 - Nov-27-2019

Works!
Thanks.
^() means doesn't include fragments that contain both symbols: '(' and ')', isn't it ?


RE: Regular expression: match pattern at the end only - Gribouillis - Nov-27-2019

[^()] means any character different from ( and )