1

for example I have txt with content

qqqqaa
qqss
ss00

I want to replace only one q at the beginning of line, that is to get

qqqaa
qss
ss00

I tried replace ^q in notepad++. But After I click replaceAll, I got

aa
ss
ss00

What is wrong? Is my regex wrong? What is the correct form?

4
  • @WiktorStribiżew Nope, this is not what I want. I want to replace only one q, not replace two qs to one q Commented Jul 19, 2016 at 7:22
  • Then please update the example, it is a bit misleading. Commented Jul 19, 2016 at 7:22
  • @WiktorStribiżew I just tried the same regex in Geany(another kind of editor) in which replace ^q works. So I thought it is a bug or a design of notepad++? Commented Jul 19, 2016 at 7:25
  • This is happening because Notepad++ does not replace them all at once, but one after the other. So after replacing first q (when there are more than one), then the next q is now at the beginning of the line. What I would do, replace ^q with space, then replace space in the beginning of the line with nothing. Commented Jul 19, 2016 at 7:26

2 Answers 2

4

The issue is that Notepad++ Replace All functionality replaces in a loop using the modified document.

The solution is to actually consume what we need to replace and keep within one regex expression like

^q(q*)

and replace with $1.

The pattern will find a q at the beginning of the line and then will capture into Group 1 zero or more occurrences of q after the first q, and in the replacement part the $1 will insert these qs inside Group 1 back into the string.

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, Wiktor Stribiżew. regarding Notepad++ Replace All functionality replaces in a loop, I will open another post which maybe related.
You know, this is not the only funny thing with Notepad++ regex. E.g. \b\w is supposed to only match an alphanumeric or _ after a non-word char, but it will match all c, a and t in cat. I already opened a bug for that some months ago, no feedback :(
1

You can use ^q(.+) and replace with $1 if you also want to replace single q's.

1 Comment

The same approach as described in my question, and a bit slower, but will work, yes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.