2

I have a STRING

"wordride plain fire "

I have tried to replace with Regular Expressions:

  • Find what: (?>(word)|\G(?<!^))\K\S
  • Replace with: $1$2$0

In Notepad ++, it does not change the text but it works in regex101 (https://regex101.com/r/aI6gE1/2), where i replaces characters after word as follows

  • First replace: wordwordide plain fire
  • Second replace: wordwordwordde plain fire
  • Third replace: wordwordwordworde plain fire
  • Fourth replace: wordwordwordwordword plain fire
  • Fifth replace: wordwordwordwordwordwordplain fire
  • Sixth replace: wordwordwordwordwordwordwordlain fire

Can you help me to see the error or give me a workaround in Notepad ++ for this purpose: replacing string after "word" character by character using a group not included in match group

Please help me

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
jhonny625
  • 266
  • 2
  • 14
  • 2
    What is the regular expression you are using? Please read [mcve] then [edit] the question to include the missing details. – AdrianHHH Jun 19 '16 at 07:58
  • Please add expected result after the replacement and add the regex as text. – Lars Fischer Jun 19 '16 at 11:22
  • Is the word known? If yes, [replace with some `::<>::`](https://regex101.com/r/wU1lX9/1) and then [replace this placeholder with the required word](https://regex101.com/r/xO2fJ0/1). – Wiktor Stribiżew Jun 19 '16 at 20:45
  • I really was looking for a solution like this but in reverse . (? =. * (Word)) replace for this $1 – jhonny625 Jun 19 '16 at 21:13
  • In reverse means you need an infinite width look**behind**, not look*ahead*. And it is not possible in Notepad++. Use Python PyPi regex module, or .NET, or VIM... Do you want exactly [this](http://regexstorm.net/tester?p=(%3f%3c%3d(word).*).&i=wordride+plain+fire+&r=%241)? In VIM, I guess, it is something like `:%s/\(\(word\).*\)\@<=./\1/g` – Wiktor Stribiżew Jun 24 '16 at 11:29
  • thanks i just switched to csharp but i have a problem http://stackoverflow.com/questions/38088949/because-this-code-dont-work-in-csharp – jhonny625 Jun 29 '16 at 01:52
  • 1
    Why did you mark the answer below as accepted if it does not work for you? Try [`(?sm)(?<=^(work\d+).*?(?>\r\n|\r?\n))(\s*\S+)\s+[\t\p{Zs}]+(&[\d.]+)?` and replace with `$1$2 $3`](http://regexstorm.net/tester?p=(%3fsm)(%3f%3c%3d%5e(work%5cd%2b).*%3f(%3f%3e%5cr%5cn%7c%5cr%3f%5cn))(%5cs*%5cS%2b)%5cs%2b%5b%5ct%5cp%7bZs%7d%5d%2b(%26%5b%5cd.%5d%2b)%3f&i=work1%26title%0d%0a%0d%0avalue2%26ame2+++++++++++++++++++++++++++++%2623.04+%0d%0a%0d%0avalu4%26name4+++++++++++++++++++++++++++++++%2681.36+%0d%0a%0d%0avalue1%26name1++++++++++++++++++++++++++++++++%2681.36+&r=%241%242+%243). – Wiktor Stribiżew Jul 11 '16 at 07:06
  • ok thankyou , i accept your response – jhonny625 Jul 11 '16 at 20:04
  • anyway, notepad ++ had many bugs, so I switch to csharp thank you for the advice – jhonny625 Jul 11 '16 at 20:06
  • You should undelete the http://stackoverflow.com/questions/38088949/because-this-code-dont-work-in-csharp, or change the current question and re-tag it. I'd rather you keep this one open, and undelete the deleted question. – Wiktor Stribiżew Jul 12 '16 at 08:00
  • I have added an answer specific for Notepad++ and this current task. – Wiktor Stribiżew Jul 13 '16 at 07:40

2 Answers2

1

The answer is yes, it is possible to do with Notepad++ BUT only with the help of a PythonScript plug-in.

Get the plugin ready, and create the following script:

import re

regex = r"^(word)(.+)"

def process_match(match):
    return "{0}{1}".format(match.group(1), "".join([match.group(1) for x in list(match.group(2))]))
editor.rereplace(regex, process_match)

The ^(word)(.+) pattern will match a line with word at its start into Group 1 and all the rest of the line into Group 2.

The "{0}{1}".format(match.group(1), "".join([match.group(1) for x in list(match.group(2))])) will paste the Group 1 value into the result first (see format(match.group(1)) and then "".join([match.group(1) for x in list(match.group(2))]) will replace each character in Group 2 with the value in Group 1.

This text:

word1
word1 2
wordride plain fire 

will turn into:

enter image description here

NOTE: You can control how many chars after word are replace with word by adjusting (modifying) the (.+) pattern.

Community
  • 1
  • 1
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
0

It's hard to understand exactly what you want to do but the following is working based on your examples:

  • Find: ^((word)+).
  • Replace with: $1$2
Jean-Francois T.
  • 11,549
  • 7
  • 68
  • 107