Regex Select Two Delimiter (start end) then apply a)line joint b)suffix characters
-
Hello,
I need line joining and <string> add between delimiter, already got help from @guy038 for selecting from to string and select all the text inside and applied line joint. Now i need to select the lines / text whatever inside between delimiters. Example a)! b).The line one the line twi the line 3 the lin4, four ! 22 some paragrapg text needs to be joind also suffix something after a dot.
Output A Required (ctrl+j) applied between ! (\d+) and .
The line one the line twi the line 3 the lin4, four ! 22 some paragrapg text needs to be joind also suffix something after a dot.
Output B Required to add some string at the end of ! and .(dot)
The line one the line twi the line 3 the lin4, four ! 22<strings added> <span added> some paragrapg text needs to be joind also suffix something after a dot.<span added>
Thanks.
-
You have already been shown examples of how to use regex to manipulate text, and those examples have been explained to you. If you have specific questions about the syntax, feel free to ask. Otherwise, before asking us to do you work for you, you need to at least try to solve the problem yourself, and if you have questions, show us what you tried, and explain why you thought it should work.
This forum is not your personal “get free regex writing any time I want it” service.
----
Do you want regex search/replace help? Then please be patient and polite, show some effort, and be willing to learn; answer questions and requests for clarification that are made of you. All example text should be marked as literal text using the
</>
toolbar button or manual Markdown syntax. To makeregex in red
(and so they keep their special characters like *), use backticks, like`^.*?blah.*?\z`
. Screenshots can be pasted from the clipboard to your post usingCtrl+V
to show graphical items, but any text should be included as literal text in your post so we can easily copy/paste your data. Show the data you have and the text you want to get from that data; include examples of things that should match and be transformed, and things that don’t match and should be left alone; show edge cases and make sure you examples are as varied as your real data. Show the regex you already tried, and why you thought it should work; tell us what’s wrong with what you do get. Read the official NPP Searching / Regex docs and the forum’s Regular Expression FAQ. If you follow these guidelines, you’re much more likely to get helpful replies that solve your problem in the shortest number of tries. -
@PeterJones Dear sir, I agree the point you have mentioned “free regex writing”. Before asking any help on this forum I will spend many days. I will search all the forums like stack etc. Even after searching many days, have searched Notepad++ forum also for start and end tag for delimiter (special characters) Then I will put my request here. Because The understanding is very easy here. That’s why raised my hand for help. Also, the Notepad++ community well aware of these codes.
When last time uploaded images, forum members responded, code to be uploaded not images. That’s why I avoided uploading screenshots.
I have tried a lot (not hours in days) before putting any Message Under Help wanted. I will not disturb unnecessary. If you understand my query then please provide a valuable reply. Thanks. -
@Ohm-Dios said in Regex Select Two Delimiter (start end) then apply a)line joint b)suffix characters:
When last time uploaded images, forum members responded, code to be uploaded not images.
Of course. If you are supplying text, then actual text is the best, like you did. The italic stuff (the stuff below the
----
) was my boilerplate, where reading that advice is good general advice.Before asking any help on this forum I will spend many days. I will search all the forums like stack etc. Even after searching many days, have searched Notepad++ forum also for start and end tag for delimiter (special characters) Then I will put my request here
But you don’t show us anything that you tried, or why you tried it. Thus, as far as we know, you are just asking us to do your work for you, without having tried anything. By showing regexes you previously tried, and why you thought they would work, it helps us understand what your thought processes are, so that we can help you move your thoughts in the right direction.
I think the reason you are failing in your searches is because you are trying to find someone who has had the exact same circumstances as you… and it is statistically unlikely that you will find someone in your exact circumstance.
Instead, you need to learn what each of the pieces/tokens in a regular expression do, and how to merge those small pieces together into a real regular expression.
The only way to do that is by reading the manual (the usermanual goes through each of the regex tokens… it’s linked in my italic block above), and by experimenting with the regular expressions yourself.
For example, if I wanted to find the period at the end of a line, and add
<span added>
after that period, I would search for\.\h*$
– when translated:\.
means “find the literal dot” (need the \ because.
means “any character” to a regex)\h*
means “find zero or more horizontal space characters” – because I am not going to assume that your data doesn’t have spaces between the period and the end of line$
means find the end of the line
For the replacement, I would use the literal
$0<span added>
$0
means take everything that matched and include it in the replacement (so you don’t delete the.
)- the rest is just literal text.
You could do similar for the exclamation point
!
replacement, but I’ll leave that as an exercise for the reader.If you want to continue to get our help, you are going to have to show that you are willing to learn and trying to work it out on your own. So far, you haven’t shown that.
I have given you enough advice for you to understand how to show that you are trying to learn, and even taken you a long way down the path toward solving your immediate problem with my example regex and explanations. But until you start showing more effort, you are going to find it harder to get help here.
-
@PeterJones Dear sir, Thanks Sure, I will follow your guidelines. I will complete this Special character regex and in future I will provide all the ways which tried. My problem is I will put more effort but unable to express. However, thanks for your Immediate Response. Have A good day!
-
@PeterJones Sir, I have tried to merge the lines. Find regex
(?<=!)[\s\S]*?(?=\.)
Matches the lines . But when merging (line joining) Using Replace function
?1\x20
it removes all the lines completely. Please look in to that. I need to join the Lines. .
-
The problem: You have no group1 in your FIND. But your replace uses the conditional, so it says “if group1 matches, replace with the space character”. There is no group one, so the conditional is false, so it gets replaced with nothing.
If you put parentheses around the stuff between the
!
and.
, then that will put it into group1.You also had a restriction in your original text (where you wanted a join-equivalent regex “applied between ! (\d+) and .”), but the example regex didn’t include that requirement, so I think you haven’t fully understood what you’re trying to use.
But the logic of your FIND won’t help you find the newlines and replace them with spaces, because there’s nothing in your regex that talks about newline sequences, so you’ve got a more fundamental problem than either the group1 or the lack of the check for the digits after the !.
Trying to find and replace all the newlines between a start marker and an end marker sounds like one of Guy’s generic regexes that he’s shared, but I’m not an expert at finding and applying those. For now, maybe @guy038 could point you to the post with the right generic pattern to use for that. (With a lot of effort, I could hack something together, but it wouldn’t cover all the conditions that Guy’s generic ones would get right)
Aside to @guy038: it would be nice to start a series of blogs where you reiterate one of the generic expressions that you and Alan develop in reply to questions, and go through in detail when and how to use each of those generics – kind of like your presentation post in the question answers, but cleaned up, and maybe with a simple example of what it would find/replace. If you did that, then maybe even add a table-of-contents post in the regex FAQ that helps users find an applicable generic (ie, just a list of the names of each blog post and a link to that post) – since you have moderator powers, you could add a new link every time you make a new one of the generics. I think that would make all your excellent work on the generic patterns much more findable than they currently are, and more likely to be referenced and used in the future. Of couse, that’s me volunteering you for more work, which I’m sure you don’t need. ;-)
-
@PeterJones said in Regex Select Two Delimiter (start end) then apply a)line joint b)suffix characters:
For now, maybe @guy038 could point you to the post with the right generic pattern to use for that.
This is probably a good place for finding that: https://community.notepad-plus-plus.org/post/62799
But here’s a problem:
(In general,) Posters to these types of questions don’t want to put the effort in to learn this stuff, nor read some well-written things and then try to apply them to their own need. They just want to be given the exact answer they need now, Now, NOW…GIVE IT TO ME!!!So lately we’ve taken the “show me what you’ve tried in order to get more help” approach. Well, this often causes posters to just spit out some crazy thing that could never work, say that it kinda worked, as a token effort to get to that next phase of giveme, Giveme, GIVEME!
Aside to @guy038: it would be nice to start a series of blogs where…
So @guy038 could put a lot of effort into the “blog” thing you describe (and I would appreciate that), but as far as giving the impatient a vehicle to their own solution, it probably isn’t going to do it.
If there is a good solution I don’t know what it is.
-
@Alan-Kilborn said in Regex Select Two Delimiter (start end) then apply a)line joint b)suffix characters:
This is probably a good place for finding that: https://community.notepad-plus-plus.org/post/62799
Thanks. I’ve now bookmarked that one. But a title of “Changing Data inside XML elements” won’t remind me that the intent of the bookmark is “Generic regex for changing specific text between START and END indicators”, so I’ll never find that bookmark when I want it. (I tried for a while to maintain a list of useful links on my own website, but it requires going over there to look at the index, and then bringing the info back here, which is an extra step of hassle which I never seem to follow through on; and I haven’t bothered adding to it in a couple years)
as far as giving the impatient a vehicle to their own solution, it probably isn’t going to do it.
My thought was that it would make it easier for me (and you) to link to "here’s a well-described generic solution; now you can put in the effort to apply it to your situation. And that will increase awareness, both for the original poster and for anyone who subsequently finds a topic, that there are generic patterns which work with most search/replace ideas, when you step back to the abstract rather than the specific. Whether they apply that awareness to gaining knowledge and experience is out of my control.
I just don’t like the feeling that I’ve left a question in a state where a future reader would say, “oh, look, another reply where they don’t actually answer the question”. Being able to say, “here is a good starting point; if you have trouble customizing that for your situation, come back and ask specific questions”. And as much as I like pointing people to the FAQ and npp-user-manual.org, there’s an awful lot to wade through to get a specific solution; starting from a generic base might make the effort less daunting, and thus more likely to be used
If there is a good solution I don’t know what it is.
I know we can’t force people to learn. But I like to be able to give them every opportunity. And having named links with the generic solutions would help me to help others.
—
Anyway, back to the original question: @Ohm-Dios ,
-
you have now been pointed to the generic expression – follow Alan’s link – which can easily be customized to look for a newline sequence (in regex search syntax🛈, it is represented by
\R
) as the FR, and the BSR would look for your exclamation point and digits, and the ESR for the period. -
I gave you a way to add text after the period. That could be easily modified to add after the exclamation point instead.
So, with those two methods, you should be able to solve your problem yourself.
-
-
@PeterJones said in Regex Select Two Delimiter (start end) then apply a)line joint b)suffix characters:
My thought was that it would make it easier for me (and you) to link to “here’s a well-described generic solution; now you can put in the effort to apply it to your situation…”
And my other thought was, by putting it in the blog section, and not the FAQ section, we could take the meta-discussions about improving the generic expressions (the tangents that we often get into with Guy over the best way to express it) without cluttering up the original question. (Like here, where we’ve cluttered up the OP’s thread with a meta-discussion about where it is best to separate meta-discussions about generic regexes, and how best to answer questions that could easiest be answered by a quick pointer to the appropriate generic regex.)
-
Hi, @ohm-dios, @peterjones, @alan-kilborn and All,
Again, @ohm-dios, if you had studied these pages, for instance :
https://www.regular-expressions.info/brackets.html
https://www.regular-expressions.info/replaceconditional.html
You would have understood, at once, why your search regex, without any group
(?<=!)[\s\S]*?(?=\.)
( BTW, you can simplify it as(?<=!)(?s).*?(?=\.)
! ) and your replacement regex?1\x20
were incompatible !How can I tell you ? Just an image : let’s imagine that I’m in England or America and I’m a bit lost, looking at a map. I take my pocket bi-lingual dictionary and verify that the French sentence “
Pouvez-vous me dire où cela se trouve sur ma carte ?
” can be translated, in English as “Can you show me where it is on the map ?
” I do not need to know the language to tell that sentence in English. Just my approximative pronunciation !! This behavior is appropriate as I’m away, on vacation for 15 days, only !But, If you intend to really learn English language, the best bet is to go abroad, to United Kingdom or America, and spent a minimum of one year to feel comfortable with the language ! It exactly the same for any subject and for regular expressions too !
As long as we’ll give you the right regex solutions for particular cases, you will not learn anything ! You need to learn basic structures, first, with regex tutorials ! Then, you’ll be able to understand exactly why you are using that syntax and not an other one ;-))
But, as I feel a bit sorry for you, here is a regex solution, derived from the general method, described in this post :
https://community.notepad-plus-plus.org/post/62799
Also , an other example in the P.S. section, at the end of this other post :
https://community.notepad-plus-plus.org/post/62837
So, the regex S/R is :
SEARCH
(?s)(!|(?!\A)\G)((?!\.).)*?\K\R+
REPLACE
\x20
- Paste the text, below, in a new tab
The line 1 The line 2 The line 3, WITHOUT any change as it does NOT contain any EXCLAMATION MARK and DOT The line 4 ! contains some lines which need to be joined till the nearest DOT character. The line 5 is NOT changed too as it does NOT contain an EXCLAMATION MARK, although it does end with a DOT. ~~~~~ The line 1 The line 2 The line 3, WITHOUT any change as it does NOT contain any EXCLAMATION MARK and DOT The line 4 ! contains some lines which need to be joined till the nearest DOT character. The line 5 is NOT changed too as it does NOT contain an EXCLAMATION MARK, although it does end with a DOT.
-
IMPORTANT : Move back to the very beginning of your file (
Ctrl + Home
) -
Type in the regex S/R above
-
Click on the
Replace All
button
Et voilà !
Comments are included in the sample text. After replacement, we get this text, where the line
4
, only, has been changed :The line 1 The line 2 The line 3, WITHOUT any change as it does NOT contain any EXCLAMATION MARK and DOT The line 4 ! contains some lines which need to be joined till the nearest DOT character. The line 5 is NOT changed too as it does NOT contain an EXCLAMATION MARK, although it does end with a DOT. ~~~~~ The line 1 The line 2 The line 3, WITHOUT any change as it does NOT contain any EXCLAMATION MARK and DOT The line 4 ! contains some lines which need to be joined till the nearest DOT character. The line 5 is NOT changed too as it does NOT contain an EXCLAMATION MARK, although it does end with a DOT.
Best Regards,
guy038
P.S. :
BTW, fellows, when I was 25 years old, about, I spent two years, near London. One day, I remember, when I woke up, I was very surprised to realize that, in my recent dream, I had spontaneously answered other people, in English ! I then thought that if I spoke in English in my dreams, I was on the right track. Some automatisms were already at work ;-))
P.P.S. : @peterjones, Your idea of using the blog section to discuss generic regex syntaxes is interesting. But, I"ll reply later.because I first have to reply to Scott after testing the new build of @sasumner, allowing the
\K
syntax with thestep by step
replacement ! -
Re: First thanks to @PeterJones, @Alan-Kilborn, @guy038. And sorry again for showing my lack of coding. I am an Electrical Engineer almost 14 years in that filed. Last few years into computer programming learning and studying on my own and one day found notepad++ after that I must say I am addicted to that. Whenever I have problems I refer forums(including notepad++) and study materials. I usually spent a lot of days for finding solution. After everything fails then only I will put my query here. From now onwards I will spend more efforts to studying and understand. @guy038 Dear sir, You are not only providing a solution, Its Love.
Again all, Sorry for Taken your valuable times and assure you I won’t come again without 100% tried my self after studying the required materials.
Best Regards,
Ohm-Dios