3

Hi i have a dataset of strings, and some strings have mixed words such as below:

    سلام12World
    دوربینdigital
    سال2012good

... and my desired output is :

   12 سلام world
   دوربین digital
   2012 سال good

here is my code :

 def spliteKeyWord(str):
     regex = r"[\u200b-\u200c]|[0-9]+|[a-zA-Z]+\'*[a-z]*"
     matches = re.findall(regex, str, re.UNICODE)
     return matches

but this code doesnt show my desired output. Is it possible to get something like that output?

get data
  • 101
  • 6

2 Answers2

1

You can use re.findall with an alternation pattern:

def spliteKeyWord(s):
    return re.findall(r'[\dA-Za-z]+|[^\dA-Za-z\W]+', s, re.UNICODE)
blhsing
  • 91,368
  • 6
  • 71
  • 106
0

Referencing this question, you can use this regex to parse non-ascii characters:

words = ['12سلامWorld','دوربینdigital','2012سالgood']

for w in words:
    re.split(r'([^\x00-\x7F]+)', w)


# ['12', 'سلام', 'World']
# ['', 'دوربین', 'digital']
# ['2012', 'سال', 'good']

This will split everything between the non-ascii words.

r.ook
  • 13,466
  • 2
  • 22
  • 39