need of explanation of find and replace with option regex
I have the follow problem:
I have a list which contains several taxes for each class of taxes.
I want to extract the classes I don’t need and I have the following possibility for extracting:
I have 5 lines,the first line starts always with the string II and the last line ends always with Steuerklasse VI, everything between , inclusive II and Steuerklasse VI has to be deleted,
which means the searching has to involve 5 lines.
I tried to set the option regex and used several possibilities with ^ and $ and .* in between or with \r, but
either I get the hole text or I get the message “Can’t find text …”
How can I make notepad++ to find this special text and replace it?
Her is an example of a snippet, I wish to replace, I’m highlighting the beginning and the end of the text,
the end of each line I make bold and italic
II 658,50 ist nicht möglich30,4949,8925,0440,9819,8732,5214,98 24,52 10,37 16,97 5,76 9,88
III 404,66 22,2536,42 17,81 29,14 13,49 22,07 1,50 15,25 0,00 8,94 0,00 3,66 0,00 0,00
IV 713,91 39,2664,25 36,29 59,39 33,39 54,65 30,56 50,0127,80 45,50 25,11 41,10 22,49 36,81
V 1.112,8361,20100,15 Keine Berücksichtigung von Kinderfreibeträgen in den Steuerklassen V und VI
VI 1.149,0863,19103,41BVSP:374,75 ‐ III:374,75TAGZ:334,33 BVSP / TAGZ nicht anzuwenden bei Steuerklasse VI
Can I attach a screenshot to my question?
thanks for the tip with the link to imgur and other webdrives, here is the image:
I tested it with Peter’s suggestion as well and I got the same errormessage.
I can give you the whole file, because it is no secret - it’s the latest taxlist for wages and you can get it in the the web. But it is too big for my purpose and you can get it only as an pdf-file. I converted it into a textfile and try to shorten it, because I’m only interested in taxclass I (not married, no bonus for children, no additional tax for church/religion)
Befor I can import it to an xls(x) file, there is a want for a slight trimming for a better import, because there are too many irregularities for import.
And here is the file
if you can’t click the link, copy the url below and paste it to your browseradress
The text link will be available for one week from today.
Thank you for your support to all
!(https://i.imgur.com/IlpoqlI.jpg)to embed it – the initial exclamation point will allow the forum to render the image, saving us from having to click to the site:
In the first image, you can see that your roman-numeraled lines are all indented. The regex assumed that the
IIwere the first characters on the line. It’s easy to fix:
[\x20\x09]*will match 0 or more spaces or tabs between the start-of-line and the
II, so it won’t matter whether it’s space-indented, tab indented, or not indented. (I wanted to use the
\wwhitespace escape, but it also matches newline, even with
If there’s any possibility of trailing spaces after the
VI, you might want to use
See why posting actual data is a good thing? It allowed the whitespace on the lines before the
IIto be seen by readers of this thread! :-)
And the problem to be detected and solved–quickly! :-D
here is the new embeding of the image:
Thanks to Peter for this tip
Image just posted is of dubious value; @PeterJones already posted it as well as solved your original problem…or is there something new to keep this going?
here is the new embeding of the image:
Thanks to Peter for this tip
I tested the new suggestions, but I get always Can’t find the next …
It seems, as if Notepad++ think the searching syntax is part of a text, not an order.
I tried it with option advanced (in German “erweitert”) and with option regex (in German “reguläre Ausdrücke”) I was trying Peters last two expressions one by one by copy and paste, the problem is not solved …
here is the last sample:
It found the text highlighted text for me
Not totally sure that I’ve found out the problem, but I would advice to tick, in the Suchen dialog, the
Am Ende von vorne beginnenoption (
Wrap aroundoption in English )
With that option set, when you begin your search, for instance, at the middle of your file, The regex engine first searches occurrences from that location to the very end of your file. Then, it go back to the very beginning of the file and continue searching until the initial location of your cursor, before the search/replace operation !
I tried that option too, but I got no result …
Is there a setting in the programm itself, which is restraining the search-routine to recognize the syntax as a syntax and not as a part of a normal text?
Thats my hunch, that the searchroutine of np++ can’t recognize the syntax anyway or simply ignores the fact, that it is a syntax ;-)
Here is the new sample with option Wrap around:
thanks for your patience to all, I don’t know, why all your suggestions don’t work in my version of npp++
when I use the same options you have suggested and insert the syntax into the field “search what” by copy and paste.
I compare the syntax, which you have written down with the one I have put it copy and by paste into the field every time before I push the button “Find next”. I don’t dare to push the button “replace” because I don’t want to purge the file.
The screenshot you showed is back to using the old regex, which didn’t allow for spaces before the
II, and required a newline at the end (so won’t match the very last occurrence) – we already showed that was wrong for your data, and explained why. Please try it again with one of the new ones, like
I know you haven’t asked for it, but I assume if you ever make my regex match, you’ll soon be noticing something I saw.
If you use the regex
(?s)^[\x20\x09]*II\x20.+?Steuerklasse\x20VI[\x20\x09]*$on the file quoted here (which is a simplified version of your file):
111,11I Header II 222,22 III 333.33 IV 444.44 V 555.55 Steuerklassen V und IV VI 666.66 Steuerklasse VI 111,11I Header II 222,22 III 333.33 IV 444.44 V 555.55 Steuerklassen V und IV VI 666.66 NoMatch VI 111,11I Header II 222,22 III 333.33 IV 444.44 V 555.55 Steuerklassen V und IV VI 666.66 Steuerklasse VI 111,11I Header II 222,22 III 333.33 IV 444.44 V 555.55 Steuerklassen V und IV VI 666.66 NoMatch VI 111,11I Header II 222,22 III 333.33 IV 444.44 V 555.55 Steuerklassen V und IV VI 666.66 Steuerklasse VI
Then the second match will be the second and third “paragraph” (as shown in the left side of the image below). (I have defined “paragraph” to mean the entire 6-line grouping)
However, if you modify the regex to
(?s)^[\x20\x09]*II\x20(?:.(?!^[\x20\x09]*II\x20))+?Steuerklasse\x20VI[\x20\x09]*$, it will match just the third “paragraph”, as seen in the lower-right, which I am assuming what you’ll eventually want.
.+?from the original regex will match any character, one or more times. By replacing that with
(?:.(?!^[\x20\x09]*II\x20))+?, I am able to restrict it to match any character, one or more times, as long as that character isn’t followed by
IIat the beginning of a line. To break it down:
(?:___)= this wrapper says "match
___as a group, but (if also using a replace string) don’t capture it into the
$1". (You don’t say whether you’re ever going to try a Replace to go with this Find What, so I thought I’d make it safe
(?:___)+?= match one of more of this group, but make it as short as possible (so if you’ve got two matching "paragraph"s in a row, it won’t be greedy and will only highlight one “paragraph” at a time)
- Moving inside, the dot
.still matches one character.
.(?!___)= this second parenthetical is a “negative lookahead assertion” (as indicated by the
?!). Together, this means "look for any one character, as long as it’s not followed by the
___. The lookahead does not “use up” any characters, so
.(?!___)still only matches one character
- By using
^[\x20\x09]*II\x20(which was what we used to define the start-of-match, earlier) as the
___in the negative lookahead, we are saying “we don’t want our standard start-of-match sequence to be anywhere inside our match”.
(I had seen this problem before I made yesterday’s post, but I didn’t have a solution at that point, so wasn’t going to mention it. :-) I thought I was going to have to hand it off to @guy038 if @Andrea-Seyfarth asked about it… but I thought of the negative lookahead while lying awake in bed before my alarm went off this morning. I tried it as soon as I could, and it worked… Hopefully this helps.)
Here are two regexes :
Regex A :
Regex B :
that I tested against the text below :
111,11I Header I 000.00 II 222,22 III 333.33 IV 444.44 V 555.55 Steuerklassen V und IV VI 666.66 Steuerklasse VI VII 777.77 Steuerklasse VII VIII 888.88 Steuerklasse VIII 111,11I Header I 000.00 II 222,22 III 333.33 IV 444.44 V 555.55 Steuerklassen V und IV VI 666.66 ----- NO MATCH 1 ----- VI VII 777.77 Steuerklasse VII VIII 888.88 Steuerklasse VIII 111,11I Header I 000.00 II 222,22 III 333.33 IV 444.44 V 555.55 Steuerklassen V und IV VI 666.66 Steuerklasse VI VII 777.77 Steuerklasse VII VIII 888.88 Steuerklasse VIII 111,11I Header I 000.00 II 222,22 III 333.33 IV 444.44 V 555.55 Steuerklassen V und IV VI 666.66 ----- NO MATCH 2 ----- VI VII 777.77 Steuerklasse VII VIII 888.88 Steuerklasse VIII 111,11I Header I 000.00 II 222,22 III 333.33 IV 444.44 V 555.55 Steuerklassen V und IV VI 666.66 ----- NO MATCH 3 ----- VI VII 777.77 Steuerklasse VII VIII 888.88 Steuerklasse VIII 111,11I Header I 000.00 II 222,22 III 333.33 IV 444.44 V 555.55 Steuerklassen V und IV VI 666.66 Steuerklasse VI
Acatches the entire FIVE lines, beginning with Roman number
IIand beginning with Roman number
VI( with possible horizontal blank characters before ), whatever their contents
And the regex
Bcatches the entire FIVE lines, beginning with Roman number
II( with possible horizontal blank characters before ) and ending with the string Steuerklasse VI, ONLY IF the string
Headercannot be found, at any position, of the smallest multi-lines sequence of characters, after the regex
\h*II\x20till the regex
So, Peter, as you can see, I used the negative look-ahead
(?!Header), which is tested at any position of the
.=> the syntax
((?!Header).)+?. Note also, by I preferred to get the entire lines, with their End of Line chars ! So, when the replacement zone is empty, it does not remain any blank line, afterwards :-))
I also, used the alternative
(\R|\z), just in case the very last line would be a line
VI 666.66 Steuerklasse VI, without any line-break !
The word “Header” was my invention, and not in any of @Andrea-Seyfarth’s examples, so that shouldn’t be used for a regex we suggest to her. Sorry for muddying the waters with my example file.
I like your cleaner
\hfor the horizontal space (that escape sequence hadn’t yet stored in my long-term regex memory; some day, maybe even today, it will).
Thanks for continually sharing your regex expertise with us. I’m always amazed by your expressions, and the quality of your explanations.
Hopefully, we’ve helped Andrea in the process. :-)