Regex should not match numbers inside parenthesis

Viewed 221

I have a passage of verses and they are numbered. I want each numbered verse in separate line so I add a new line before them but I have some parenthesis that also have numbers. They too are replaced by new lines. I don't want to match the numbers inside parenthesis. I used

$_=~s/(\d+)/\n$1 /gs;

with this input:

1Hello2Hai (in 2:3) 3hi 4 bye

but it replaces the numbers inside paranthesis also.

Required output :

1 Hello
2 Hai (in 2:3)
3 hi
4 bye

Actual output:

1 Hello
2 Hai (in
2:
3)
3 hi
4 bye

How do I construct the regex so that it doesn't match inside parenthesis. I use perl for the regex.

2 Answers
Related