Regular Expression: Urgent Need Plz Help
-
Got a new problem.
Here is how it is.one
twothree
four
football:
test
itfive
six
this is a football:
soccer
ballLook above, the word football has this symbol :
I want to delete this symbol which is located 2 lines under ========= but not any other : which is located else where i the text. I need to change only 2 lines under =========Please help.
-
So, if a colon occurs with two lines after the ========== line and before the line that contains the colon, you want to delete that colon? If there are more restrictions, you will need to provide details.
Find What :
(?-s)==========\R.*\R.*\R.*\K:
Replace With :(?-s)
= turns off.
matching end-of-line (EOL) characters\R
= this matches EOL:\r
,\n
, or\r\n
==========\R
= find the ===-line, including its EOL.*\R
= match exactly one whole line (including EOL), and.*
grabs as much as it can. Thus.*\R.*\R.*
finds two whole lines, plus some or all of the next\K
= “throw away” everything before there: all the stuff up to\K
must match, but it won’t be included in the official “search and replace match”:
= match the : character. Since\K
removed everything else before that from the match, the:
character is the only character in our search and replace match
By replacing whatever was matched (ie, that colon) with nothing, you will delete the colon, but only if there are two lines between the ==== and the line with the colon.
At some point, you need to actively start learning regular expressions on your own, rather than just piling new requirements in the help forum. This FAQ posting should give you a good place to start.
A note on how you’re presenting yourself. People in help forums are more likely to help you if you are polite and patient, and show a willingness to learn. Instead of just giving a new set of requirements, it would be nice if you had done something like: “thanks for the previous help. I have more I want to do: here’s what I need; here’s what I’ve tried; here’s how it didn’t match my expectations; can you please help me figure out what I’ve done wrong”. This shows a willingness to learn, and do it yourself, and that you are here for help, rather than to get us to do the job you’re paid for for free while you reap the benefits. (Admittedly, your request may not be for your job, but we have no way of knowing… and usually “urgent” things aren’t for a simple hobby.)
If you keep demanding, and saying “urgent”, and using “133T speak”, and starting new threads to ask what you’ve already asked, and adding new requirements without showing an effort to do it yourself and to learn, then those of us in a position to help you are likely to stop wanting to be helpful.
-
Thanks for your suggestion, you are right. Accept my appologies. I needed the first code very urgently and it helped me on spot.
I am not person of code, I have very basic understanding of codes.
I tried your code but i was not able to delete the symbol :
If you are still not mad at me :(
plz helpBest Regards
-
I’ll step in here (in case Peter might not respond straight away).
You say the supplied expression didn’t work, yet I grabbed the example you supplied and the expression and it found the 2
:
in the example.You can help by supplying the code as you typed it. This is not as stupid as it sounds as sometimes what you thought you typed isn’t what you actually have.
I also tried to select the “ball” in the 2 lines by using the expression
Find:(?-s)==========\R.*\R.*\R.*\Kball
Note that I replaced the:
withball
in the original expression.See if that gives you any different answer. If it does then replace the
ball
with:
and try again. The more info you can supply will help everyone to help you.Terry
-
@Rumi-Balkhi said:
I needed the first code very urgently…I am not person of code…have very basic understanding
If you want to do this type of data manipulation, your best way to achieve that end is to slow down and learn it…get yourself another tool in your toolbox, so to speak.
-
I’m not on this forum 24/7, so answers won’t always come immediately.
I’m not mad.
I’m just trying to help you … and part of helping you is helping you learn how to help yourself, and how to ask effective questions. I was browsing someplace else yesterday, and found this: “How to ask questions the smart way”, which has some good general advice for asking questions in online forums.
The regex I showed matched and eliminated the two “:” characters from the exact data you posted. If it didn’t do so for you, first, I’d check if you remembered to enable
☑ Regular Expression
. And, if your cursor isn’t at the beginning of the document, you probably also want☑ Wrap around
.If that still doesn’t work, as @Terry-R suggested, supply the regular expression exactly as you typed it. When you embed it in the forum, you can wrap it in `-characters, so it looks like, `(?-s)==========\R.\R.\R.*\K:`, and will render like
(?-s)==========\R.*\R.*\R.*\K:
. Make sure you copy/paste, rather than retyping – it is easy to accidentally mistype what you think you see.Also, supply the exact text you’re trying to run the search-and-replace on: to make sure the text doesn’t get interpreted as Markdown (ie, to make sure the forum doesn’t try to use characters from your text as formatting characters), you can either indent every row four spaces or one TAB (open textfile in Notepad++, highlight it all, hit TAB, copy, then paste in the forum), or you can use a trick that @Scott-Sumner explains in his excellent “Markdown code on this forum”, which is to start it with a line that’s ```z and ends with ```
```z one two ========== three four football: test it ========== five six this is a football: soccer ball ```
That same article also explains how to embed an image, using the syntax
![](https://i.imgur.com/pWS5fis.png)
. To take a screenshot, you can useAlt+PrtScrn
to grab the current window, orPrtScrn
to grab your whole desktop, and then paste it intomspaint.exe
or your favorite bitmap editor, and save it to your drive. Or use Windows Snipping Tool (comes with Windows 10) to highlight whatever portion of your screen you want, andFile > Save As
to save to your drive. Once you’ve got it as a physical file, you can upload to imgur.com or some other similar file-sharing service, then embed it using the syntax I showed above.Speaking of: here’s a screenshot of my version of your text, along with the Replace window:
In these circumstances, if I keep hitting Find Next, the highlight will go from one colon to the next, then wrap back to the first. And if I hit Replace, it will remove the current colon; if I had Replace All, it will remove all the matching colons.Useful links, spelled out:
- “How to ask questions the smart way”= http://catb.org/~esr/faqs/smart-questions.html
- “Markdown code on this forum” = https://notepad-plus-plus.org/community/topic/14262/how-to-markdown-code-on-this-forum
and, from yesterday’s post,
- “FAQ Desk: Where to find regex documentation” = https://notepad-plus-plus.org/community/topic/15765/faq-desk-where-to-find-regex-documentation
Reading these, showing a willingness to learn and put in effort yourself, will go a long way to improving the quality of your questions, and thus the quality of the responses and help you get.
-
Dear Scott, Peter and Terry,
Thanks alot for your help. I tried the exact expression and got my answer. It worked very well.
I am very pleased and thankfullll to you.
-
Hello, @rumi-balkhi, @peterjones, @scott-sumner and @terry-r,
My attitude is not very courageous, as I’m stepping in that post, when everything is very well said and done !!
But, to sump up, Rumi, here is , below, a regex S/R which, both, adds your
==========
line and gets rid of any possible:
located after the wordfootball
SEARCH
(?-s)((?:.*(\R)){2}.*football):?
REPLACE
==========\2\1
So, assuming the sample text :
one two three four football: test it five six this is a football: soccer ball End of test seven eight the football match is over nine ten
It would give, after replacement the following text :
one two ========== three four football test it ========== five six this is a football soccer ball End of test ========== seven eight the football match is over nine ten
BTW, Peter, you said, in your last post that a step-by-step replacement was possible, hitting the
Replace
button.Unfortunately, Rumi, when a regex contains a
\K
syntax, most of the time, the step-by-step replacement does not work:-((
In such a case, you must use theReplace All
button, exclusively. However, any hit on theFind Next
does match each occurrence !Best Regards,
guy038
-
@guy038
Thank you for your kind suggestions. I tried them and it worked so well. Since you encouraged me, maI ask a new question.Suppose the symbol is not : its a ?
Examplewith a line that’s
z and ends with
==========
One
Two
one?
twothree
four
Any thing?
test
itfive
six
this is a question mark?
soccer
ballNow If you see every third line below ========= has a question mark. Any suggestions for this.
Thanks
-
Hi, @rumi-balkhi and All,
If I understand you ,correctly, you would like to insert a
==========
line, two lines above a line which ends with a question mark ? But you did not tell us if you want to keep the?
symbol, after replacement ! Anyway, I give the right regexes for the two cases :-))So, assuming the text :
One Two one? two three four Any thing? test it five six this is a question mark? soccer ball
The regex S/R :
SEARCH
(?-s)(?:.*(\R)){2}.*\?
REPLACE
==========\1$0
would give the following text, with the
?
still present========== One Two one? two ========== three four Any thing? test it ========== five six this is a question mark? soccer ball
And the regex S/R :
SEARCH
(?-s)((?:.*(\R)){2}.*)\?
REPLACE
==========\2\1
would give the same text, without the final
?
at the end of the third line after the==========
line========== One Two one two ========== three four Any thing test it ========== five six this is a question mark soccer ball
Cheers,
guy038
-
This is the point when you need to start reading and understanding where to find regex documentation, which I linked earlier.
This is not a professional regex-writing forum. We are users of Notepad++, who want to help others learn more about NPP (and learn more ourselves, in the process). Since Notepad++ makes use of regular expressions, we will often provide help with regex questions.
However, after this many rounds, you really need to start applying what you’re learning. Because you are learning, right? We’re not just doing your job for you. Because if we were, we’d be charging you consulting rates.
To reluctantly answer your question, “Suppose the symbol is not
:
its a?
”. The obvious choice would be to try replacing the:
from the regex with a?
. Did you try that? What happened? If you did try it, it wouldn’t have matched, so you could have said “when I tried to replace the:
with a?
in the regex, it didn’t match/highlight the question mark when I hit FIND, or replace the?
when I hit REPLACE… Why not?” At that point, you would have shown a willingness to learn how to do it, to generalize from what you’ve been shown, and to ask questions about what you tried, rather than hoping we’d do all the work for you. (Such a lost opportunity. :-( )After you asked that question, one of us could have replied, "if you follow the link to where to find regex docs, the first link there is the NppWiki:Regular Expressions page; you can search that page for
?
, and learn that in regular expressions,?
means to match 0 or 1of the last character – the last “character”, in that case, actually being the match in parenthesis just before it. If you want to match the literal of a special character in a regex, you have to escape it by putting a\
in front of it… so to match a literal question mark, you would use the sequence of\?
in your regex.So, if I take @guy038’s
(?-s)((?:.*(\R)){2}.*football):?
, which matches to a colon, I would change it to(?-s)((?:.*(\R)){2}.*football)\??
to match a question mark instead.If you’re going to be doing more regular expressions, spend some time in the list of regex documentation. It will help you.