Why is it that some regex match is not replaced one by one?

Viewed 100

I am using notepad++ and akelpad for text editing.

sample text : hello,this is a test.are you ready?

search for : (?<=[a-z,.?! ])([,.?!])(?=[a-z])

replace with : \1

replace result : hello, this is a test. are you ready?

It works as intended, only if I replace them all, but if I click on replace button, nothing happens. It navigates to the next match as if I click on find.

refer to this screenshot

It happens both in npp and akelpad. Why does this happen and what's the solution I should try?

Thank you!

2 Answers

It looks like a bug since the regex is fine.

All similar problems in Notepad++ and other suchlike editors can be solved by capturing as more as you can.

In your case, you may turn the lookbehind into a consuming pattern:

([a-z,.?! ][,.?!])(?=[a-zA-Z])
^^^^^^^^^^^^^^^^^^   

Now, when replacing with \1 , you will get the desired behavior, i.e. a space will get appended to the match value.

Demo:

enter image description here

If this is only what you want, try this ....

Find what: [,.]
Replace with: $0

Related