Deleting a string and the surrounding code.
-
Hello Everyone,
I would like to delete a string, the text before it, the text after it and the preceding line before the string and/or one line or two lines after the string.
For instance with this text:
10. STRING FROM /PSAC728 TO /PSAC729 20. SPREF /AISC-SPEC/ C12x20 30. POSS E345.996 S77.496 U399.504 40. POSE E390.996 S77.496 U399.504
and using /AISC-SPEC as my search string Notepadd++ would delete the entire code block from 10 to 30 or in other cases 10 to 40.
Thanks for your suggestions!
-
the text before it
Does that mean the text one line before it? or the text on N lines before it? or just the text on the same line that comes before the match?
the text after it
does that mean the remaining text on the same line that comes after the match? or something else?
and/or one line or two lines after the string
So how are we supposed to know when you want to match one line after and when you want to match two lines after? You gave us no hint of a rule for deciding that and/or condition.
What is a meaningful example of data, which will show us both before and after text, along with clear rules as to which extra information gets deleted, and which should be kept.
Also, are the line numbers (10, 20, etc) actually part of the text of the file? (Sometimes, questioners add dummy line numbers to their text when they really mean to just indicate the line numbers that Notepad++ shows. But since you are going by steps 10, I am thinking those are probably actually part of the text.)
Because the answers to each of those questions can drastically change the requirements that would have to be put into the regular expression designed.
----
Useful References
-
The line numbers are not part of the code.
Here is an example using “AISC-SPEC” as my search string.
By “text before” I mean the cyan highlighted text.
By “text after” I mean the yellow highlighted text.Sometimes there is one extra line on unwanted code (orange highlighted) which we need to get rid of as well.
After execution nothing should remain…
Apologies for the abstruse language.
-
Unfortunately, your picture hasn’t clarified much, and didn’t make it any easier for us to understand.
By “text before” I mean the cyan highlighted text.
By “text after” I mean the yellow highlighted text.How do we know how much text should be the “cyan” text? How do we know how much should be “yellow”? You haven’t defined it, but you’ve just said it exists.
Sometimes there is one extra line on unwanted code (orange highlighted) which we need to get rid of as well
You say “sometimes”, but you don’t tell us when it is and when it isn’t. How are we supposed know when there is “orange” text or not? You need to define a rule, not just claim it exists.
After execution nothing should remain…
And if I took your statement literally, the search string would be
(?s)\A.*\z
and the replacement would be empty. This would delete everything in your file, which is literally what “after execution, nothing should remain” means, but presumably not what you actually want.We cannot read your mind. Your example text has no meaning to us, so we cannot even try to guess what you’re trying to say using a comparison. You give us no actual text in the post, but either gave us text that had numbers which weren’t actually in your text but didn’t tell us they weren’t actually there until I asked you point blank, or else you gave us just a picture which we cannot copy/paste the text from.
Please, please, please read the Template for Search/Replace Questions that I’ve linked you to – especially pay attention to the instructions that say your file should contain multiple things that do and don’t match, and that you need to embed your text in the code tags otherwise we cannot reliably copy/paste it. If you are not able to learn how to ask your question from that FAQ, I will be unable to help you, and I doubt any of the other regulars here in the forum will be able to, either.
-
Fellow Notepad++ Users,
Could you please help me with the following search-and-replace problem I am having?
Here is the data I currently have (“before” data):
STRING FROM /PSAC6233 TO /PSAC6235 SPREF /AISC-SPEC/ W8X31 POSS E983.676 S177.024 U272.030
Here is how I would like that data to look (“after” data):
/AISC-SPEC/
I am new to notepadd++ and regex.
-
Your example data isn’t as complicated as what you’ve described earlier – it doesn’t have any of the “sometimes” that you kept mentioning – but it’s also got new features (indentation) that weren’t there before. So I virtually guarantee that after you try this, you will come back and say “it doesn’t work” – and it’s because you haven’t shown representative data. As the FAQ that you “read” to be able to quote the syntax made very clear: to get a good answer, you need to provide multiple examples, of things that will and won’t be transformed. Once again here, you just showed one example. So it’s not going to 100% meet your needs. And you are going to have to live with the consequences of your decision to provide insufficient data.
But pretending that what you said in your most recent post is literally what you meant:
FIND =
(?-s)^.*\R.*(/AISC-SPEC/).*\R^.*\R
REPLACE =$1
SEARCH MODE = Regular ExpressionThis will look for a group of three lines, where the second line contains whatever is in between the parentheses in the regex; it will replace those three lines with just the text that was stored in the regex parentheses.
If you decide that this isn’t sufficient, you are going to have to put more effort into your request. Good luck.