1
import re

txt = "(NASDaq/abnjnxd:number1) ojsnxsjsxmjosx (nasDaq:number2) (NYSE:bhdnd) (Nasdaq:eres)"

x = re.findall("NASDAQ|NYSE.*:.*\)$", txt.upper())

print(x)

Right now I am getting output

['NASDAQ', 'NASDAQ', 'NYSE:BHDND) (NASDAQ:ERES)']

while what I want in output is

['NASDaq/abnjnxd:number1','nasDaq:number2','NYSE:bhdnd','Nasdaq:eres']

Thanks in advance

3
  • Use (?:NASDAQ|NYSE).*?:.*?(?=\)). Demo: regex101.com/r/9aNcdi/1 Commented Aug 18, 2021 at 7:40
  • Use re.findall(r"(?i)\b(?:NASDAQ|NYSE)\b[^:()]*:[^)]*", txt) Commented Aug 18, 2021 at 7:44
  • Seems to me as if you want to match everything enclosed by parentheseses. Try (?<=\()[^)]*(?=\)). See it here at Regex101. Commented Aug 18, 2021 at 8:27

2 Answers 2

1
import re

txt = "(NASDaq/abnjnxd:number1) ojsnxsjsxmjosx (nasDaq:number2) (NYSE:bhdnd) (Nasdaq:eres)"

x = re.findall("(?:NASDAQ|NYSE)[^\)]+", txt.upper())

print(x)

Details

  • (?:NASDAQ|NYSE) non-capturing group matching NASDAQ or NYSE
  • [^\)]+ match all characters until )

Demo

https://regex101.com/r/Y2h7OO/1

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

Comments

1

You could use a capture group that will be returned by re.findall.

Note that to get the lowercase output, you should not use txt.upper()

You can make the pattern case insensitive using re.I

\(((?:NASDAQ|NYSE)[^()\s]*)\)

The pattern matches:

  • \( Match (
  • ( Capture group 1
    • (?:NASDAQ|NYSE) Match either NASDAQ or NYSE
    • [^()\s]* Match 0+ occurrences of any char except ( and ) or a whitspace char
  • ) Close group 1
  • \) Match )

Regex demo | Python demo

For example

import re

txt = "(NASDaq/abnjnxd:number1) ojsnxsjsxmjosx (nasDaq:number2) (NYSE:bhdnd) (Nasdaq:eres)"
x = re.findall("\(((?:NASDAQ|NYSE)[^()\s]*)\)", txt, re.I)
print(x)

Output

['NASDaq/abnjnxd:number1', 'nasDaq:number2', 'NYSE:bhdnd', 'Nasdaq:eres']

A more precise pattern could be matching an optional part that starts with a / and word characters, followed by matching : and word characters.

\(((?:NASDAQ|NYSE)(?:/\w+)?:\w+)\)

The pattern matches:

  • \( Match (
  • ( Capture group 1
    • (?:NASDAQ|NYSE) Match either NASDAQ or NYSE
    • (?:/\w+)? Optionally match / and 1+ word chars
    • :\w+ Match : and 1+ word chars
  • ) Close group 1
  • \) Match )

Regex demo

5 Comments

Works good except its also picking up matches like "NYSE ajjanxxdmdicjmdicjmdcdckdmcldcmdlmdlcmdlcdlcdmcdlc (something else)". What i need is for it to seach only for above mentioned patterns. In above mentioned patterns NASDAQ,NYSE are intechangle in all combinations.
@NipunTulsyan Do you mean you don't want to match spaces as well? See regex101.com/r/9zFWDz/1
@NipunTulsyan A more exact pattern could be \(((?:NASDAQ|NYSE)(?:/\w+)?:\w+)\) See regex101.com/r/7yuVlI/1
Could u make it so it also searches for "(" as the first character ? Everything else is fine
@NipunTulsyan It already does using \( but it is not in the capture group so it will not be part of the result returned by re.findall.

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.