0

Regular Expression to find identifiers (whts wrong in my code)

import re text = input("Enter filename : ") in_identifiers = [] tokens = [] isStr = False isWord = False isCmt = 0 token = '' for i in text: if i == '/': isCmt = isCmt+1 elif isCmt == 2: if i == '\n': token = '' isCmt = 0 elif i == '"' or i == "'": if isStr: tokens.append(token) token = '' isStr = not isStr elif isStr: token = token+i elif i.isalnum() and not isWord: isWord = True token = i elif isWord: token = token+i for token in tokens: if re.search("^[_a-zA-Z][_a-zA-Z0-9]*

quot;,token): in_identifiers.append(token) print("\nNo. of identifiers = ",len(in_identifiers)) print(in_identifiers)

14th Nov 2022, 6:51 AM
Muhammad Abdullah
Muhammad Abdullah - avatar
2 Réponses
+ 5
Muhammad Abdullah , maybe some samples input and output could help us. also a brief description would be a good idea. if it is just the regex part that causes an issue for you, just give a description of what inputs could be and what you expect to get by using regex.
14th Nov 2022, 9:20 PM
Lothar
Lothar - avatar
+ 1
Also, instead of pasting a wall of code in the question description, add a link to your code in Code Playground (use "+" button). This allows people to test and debug your code.
15th Nov 2022, 2:50 AM
Emerson Prado
Emerson Prado - avatar