I need to insert line breaks (enter marks) between a string before each new word starts.
String:
test (n) trial, experiment, check run (v) race, rush speed (n) race, sprint, rush, dash, zoom
Expected:
test (n) trial, experiment, check
run (v) race, rush
speed (n) race, sprint, rush, dash, zoom
This regular expression selects the word before paranthesis. But how do I insert an enter mark at the right place?
\w+(?=\s+((.*?)))
Update:
The answer do not apply to the actual string that I need to process. Does unicode strings treated differently by regular expression?
import re
regex = r"(\w+)(?= (?:[()])).*?"
test_str = "खत (स्त्री) पाहा : भेट मुलगा (पु) पोर‚ पोरगा‚ पोरटा‚ कारटा‚ किशोर‚ कुमार‚ कुमारिका‚ तरुण; लग्नाचा/उपवधू मुलगा; पाहा : पुत्र ‚ पुरुष (n) boy, lad, kid, urchin; पाहा : पुत्र ‚ पुरुष मुलगी (स्त्री) पोर‚ पोरगी‚ पोरटी‚ बाला‚ बाळा‚ बालिका‚ छोकरी‚ छोटी‚ बेटी‚ कारटी‚ नग्निका‚"
subst = "\\n\\1"
result = re.sub(regex, subst, test_str , 0, re.MULTILINE)
if result:
print (result)
The first line break is correct "\nखत" but the second one is incorrect at "पुरु\nष ". Third and forth are missing.
Expected:
खत (स्त्री) पाहा : भेट
मुलगा (पु) पोर‚ पोरगा‚ पोरटा‚ कारटा‚ किशोर‚ कुमार‚ कुमारिका‚ तरुण; लग्नाचा/उपवधू मुलगा; पाहा : पुत्र ‚
पुरुष (n) boy, lad, kid, urchin; पाहा : पुत्र ‚ पुरुष
मुलगी (स्त्री) पोर‚ पोरगी‚ पोरटी‚ बाला‚ बाळा‚ बालिका‚ छोकरी‚ छोटी‚ बेटी‚ कारटी‚ नग्निका‚