Replace before
-
Re: Replace specific part of the line AFTER the match
Hi, could someone help me to edit this expression so it would do the same with one difference? String I need to replace should be BEFORE the match, not after it.
Thank you!
-
If you mean you want to insert your replacement before the matched text, without deleting/overwriting any text, you might be able to get away with just adding
$0
to the end of your replacement. But I am very uncertain what you’re asking for, and since your question doesn’t have any example text, with no examples of “before” and “after” data, I have nothing to experiment with.If that’s not what you meant, you will have to put more effort into your question, so that we can actually understand what you are asking for.
Here’s a template for how to ask search/replace questions:
https://community.notepad-plus-plus.org/topic/22022/faq-desk-template-for-search-replace-questions -
Hi, details were mentioned in my previous question which I am responding to and where I was trying to replace a string after the match (link is above my post, example included).
Basically I got the anwser which worked really well, but now, I need the same but in opposite direction - previous solution worked liked this:
find: (?<=stringToMatch.{numberOfLettersAfterTheMatch}).{numberOfLettersToFind}
replace: WHATEVERnow I need something that could allow me to do the replacement BEFORE the matched string and my totally lame solution by adding minus before the numberOfLettersAfterTheMatch didnt work :(
Hello guys,
could you please help me with this little editing problem?
A have text file, in which I need to find all hits of a specific string and then I need to edit a part of the line which follows the searched string. Text file is formatted so the positions on the line of the searched and to-be-replaced strings are the same.
Is it somehow possible with regular expressions?
Thank you very much!484116b9-e4a1-47d5-883b-8575397d5427-image.png
-
The “details” in that previous post were a screenshot, which means we cannot replicate your data and try it out easily. And the details highlighted the text you wanted to require before and then the text you wanted to match/replace. You haven’t done that for this request, which means we have to guess what you mean by BEFORE and where you want that to match.
You are asking us to guess what you want, and to do all the work, and to magically come up with a solution for you.
And your description of
A have text file, in which I need to find all hits of a specific string and then I need to edit a part of the line which follows the searched string.
sounds a lot more like your original problem, than this attempt to do the “opposite”. This conflicting information makes it almost impossible to guess what you really want. If, instead, you had followed my advice and made use of the template, instead of arguing that you’d provided enough information (which you obviously had not provided enough information for at least me to understand what you want), you would probably be getting a solution in this reply, rather than an admonishment to do better.Given that the previous solution was
(?<=Z115 65241.{XXX}).{5}
, which searches forZ115 65241
and XXX more characters, then matches 5 characters and replaces them withWHATEVER
. I am going to guess (and it is only a guess, because you are actively fighting rather than just providing the information requested) that you want to instead replace 5 characters that are XXX characters beforeZ115 65241
this time.The previous solution used
(?<= ... )
as a “lookbehind” which says “these characters have to come before the matched text, but aren’t part of the match.” What you want for this one (to have the matched data come before specific text) is a “lookahead” instead:(?=...)
.So for my interpretation of your desire:
FIND =.{5}(?=.{XXX}Z115 65241)
REPLACE =WHATEVER
If XXX is 10, so you have 5 chars you want to replace, then 10 chars you want to keep, then the known sequence
Z115 65241
that you also want to keep, it could be
FIND =.{5}(?=.{10}Z115 65241)
REPLACE =WHATEVER
which would look in the “before” text
12345 123456789x Text FIVER then ten Z115 65241 and more come after Text FIVER then ten but not those characters 12345 123456789x And somewhere else Text GOING then ten Z115 65241 and this line will change too
and replace it with “AFTER” text:
12345 123456789x Text WHATEVER then ten Z115 65241 and more come after Text FIVER then ten but not those characters 12345 123456789x And somewhere else Text WHATEVER then ten Z115 65241 and this line will change too
Do you see now how easy it is to replicate my results? You just have to copy the original data and paste it into Notepad++, then run the regex that I provided, then compare it to what I got in my second batch of data. If you had done this for me, rather than guessing, I could have known whether my regex was getting the results you desired or not. Instead, I just have to guess. When you’re asking someone to do your work for you, it’s better to provide enough information that they don’t have to guess; and when they ask for clarification or more information, give them that information rather than repeating the same thing you said before.
If this isn’t what you want, either you’ll have to give in and follow the advice you’ve been given, or someone else will have to do better at interpreting your desires than I can.
-
@peterjones First of all, I’m sorry for my incompetence to provide neccessary data - I mean it. I wrote my response in a hurry and wrongly supposed that you didn’t catch the link to my previous post (sorry for that also) and thought that info in that post is enough.
None of this is your problem and I donť want to make any excuses for this behaivour…just for clarification.And BIG THANKS that you spent your time with replying despite everything above! Your guess was correct and provided solution does exactly what I need.
So sorry again and thank you also. Next time, if I’ll need some advice in the future, I’ll try to specify my needs better and provide test data in the first place.