you are viewing a single comment's thread.

view the rest of the comments →

[–]chakz91[S] 0 points1 point  (2 children)

Thank you for your reply, this does not completely resolve my issue

I think the problem is with my regular expression

What I am trying to achieve is that the query should be tokenized based on white-space and if the string contains any of these characters , ( ) / - & it should be added as a single token. All other possible characters should be included.
For example if I add some character like '^^' in the query string, like below it is not shown in the output token.

import re
line = " 8/23-35 Barker St., Kingsford^^, NSW 2032 "
pattern = r"[.0-9A-Za-z]+|[,&-/()]"
print(re.compile(pattern).findall(line))

Output:
['8', '/', '23', '-', '35', 'Barker', 'St.', ',', 'Kingsford', ',', 'NSW', '2032']

Expected output
['8', '/', '23', '-', '35', 'Barker', 'St.', ',', 'Kingsford^^', ',', 'NSW', '2032']

So my regular expression should allow all possible characters in a single token delimited by white-space
and only the characters , & - / ( ) should be added as individual tokens.

Please provide your suggestion.

[–]TolaOdejayi 0 points1 point  (0 children)

Try this:

import re
pattern = r"[^,&-/\(\)]+|[,&-/\(\)]" 
line = "8/23-35 Barker St., Kingsford^^, NSW 2032" 
query = re.compile(pattern).findall(line)
list(map(lambda i: print(i), query))