Reputation: 335
I am trying to extract just words from strings as below but somehow it doesn't work, i have the current and expected output, any guidance on how to fix it?
import re
string1 = 'test,'
string2 = 'OS_LOG_ALERT("received'
string3 = 'target.\\n\");'
line1 = re.sub(r'\W+', '', string1)
line2 = re.sub(r'\W+', '', string2)
line3 = re.sub(r'\W+', '', string3)
print (line1)
print (line2)
print (line3)
CURRENT OUTPUT:-
checkivarsfDriversetLinkChangedEventDatakWiFiLinkDown
OS_LOG_ALERTreceived
targetn
EXPECTED OUTPUT:-
check ivars fDriver setLinkChangedEventData kWiFiLinkDown
OS_LOG_ALERT received
target
Upvotes: 0
Views: 69
Reputation: 71610
Try with findall
instead:
import re
string1 = 'check:ivars->fDriver->setLinkChangedEventData(kWiFiLinkDown,'
string2 = 'OS_LOG_ALERT("received'
string3 = 'target.\\n\");'
line1 = ' '.join(re.findall(r'\w{2,}', string1))
line2 = ' '.join(re.findall(r'\w{2,}', string2))
line3 = ' '.join(re.findall(r'\w{2,}', string3))
print (line1)
print (line2)
print (line3)
Output:
check ivars fDriver setLinkChangedEventData kWiFiLinkDown
OS_LOG_ALERT received
target
Note: I filtered words with 2 or more characters only to get rid of the n
Upvotes: 2