Regexp different behaviours in notepad++ - regex

I have a set of words like this
"text1
"text2
"text3
which i need to convert to a quoted text which I was able to do with stackoverflow help like this
Find: ^\".*$
replace: $0"
There are another set of words like this
text1
text2
text3
which i need to convert to 1 quoted text which i was able to do
Find: ^(.+)$
Replace: "$0"
But in this case i was able to do it only by clicking replace all and not replace.
Can anyone tell why it's happening like this and How can I achieve this without using replace all and by using only replace?

Take care of the second replacement, it will replace "text1" by ""text1"".
Use this instead:
find what: ^[^"\n]+$
replace: "$0"

It should be working with Replace All and not Replace in any case. The function can also be affected by the position of the cursor and the direction to find/replace.
To be certain, you can place the cursor at the beginning of the file, ensure that the find direction is 'Down' and use Replace All.
Now to the regex, yes, you can do it in a single replace. Use this find:
^(?:")?(.*)$
And this replace:
"$1"
regex101 demo.
The (?:")? will consume the first double quote if present so that it isn't placed in the replace later on.

There's no way you used "Replace" only once in the first case! You must have pressed it 3 times (as many as your matches).
In both situations you need "Replace all", since you want multiple global matches. When pressing the "Replace all" button it's like using Perl's /g & /m modifiers in your substitution, you can experiment here to see what I mean.
BTW the use of a capture group (the parenthesis) is redundant in your 2nd example, use find: ^.+$ and replace: "$0".

Related

atom - How to replace in Atom without replacing some part of the search?

Let's say I have these lines in a file:
This and an child
That and a entrepreneur
These and a banana
This and an cookie
And I want to replace all an and a that are incorrect.
I can use an [^aeiou] or a ^[aeiou] to find all the incorrects (I think, I'm not good with regexp).
What I want is to replace the incorrects using these expressions without changing the letter after the a/an.
How can I do that?
You need to use a capturing group, (...), and then include that in the replacement
Search:
an ([^aeiou])
a ([aeiou])
Replace:
a $1
an $1

Notepad++ replace text with RegEx search result

I would like replace a standard string in a file, with another that is a result of a regular expression. The standard text looks like:
<xsl:variable name="ServiceCode" select="###"/>
I would like to replace ### with a servicecode, that I can find later in the same file, from this URL:
<a href="/Services/xyz" target="_self">
The regular expression (?<=\/Services\/)(.*)(?=\" )
returns the required service code "xyz".
So, I opened Notepad++, added "###" to the "Find what" and this RegEx to the "Replace with" section, and expected that the ### text will be replaced by xyz.
But I got this result:
<xsl:variable name="ServiceCode" select="?<=/Services/.*?=" "/>
I am new to RegEx, do I need to use different syntax in the replace section than I use to find a string? Can someone give me a hint how to achieve the required result? The goal is to standardize tons of files with similar structure as now all servicecodes are hardcoded in several places in the file. Thanks.
You could use a lookahead for capturing the part ahead.
Search for: (?s)###(?=.*/Services/([^"]+)") and replace with: $1
(?s) makes the dot also match newlines (there is also a checkbox available in np++)
[^"] matches a character that is not "
The replacement $1 corresponds to capture of first parenthesized subpattern.
I am no expert at RegEx but I think I may be able to help. It looks like you might be going at this the wrong way. The regex search that you are using would normally work like this:
The parenthesis () in RegEx allow you to select part of your search and use that in the replace section.
You place (?<=\/Services\/)(.*)(?=\" ) into the "Find what" section in Notepad++.
Then in the "Replace with" section you could use \1 or \2 or \3 to replace the contents of your search with what was found in the (?<=\/Services\/) or (.*) or (?=\" ) searches respectively.
Depending on the structure of your files, you would need to use a RegEx search that selects both lines of code (and the specific parts you need), then use a combination of \1\2\3 etc. to replace everything exactly how it was, except for the ### which you could replace with the \number associated with xyz.
See http://docs.notepad-plus-plus.org/index.php/Regular_Expressions for more info.

How to use RegEx to add "_" between two words with notepad++

I want to use Notepadd++ replace option with regular expression to accomplish this:
From: IntegrationName
To: Integration_Name
How can do this ?
My RegEx to search is: .[A-Z]
this finds: "oN"
But I don't know what to put in the replace box so it will only add "_" between the "o" and the "N"...
Another solution using lookaround assertions would be:
(?<=[a-z])(?=[A-Z])
and replace with
_
Note: The "Match case" option needs to be active, otherwise Notepad++ will find a match between every two letters.
This regex will find every position where a lowercase is on the left and an uppercase is on the right.
You can make use of capture groups. If I have to take your current attempt and edit it as little as possible, you would get:
(.)([A-Z])
This will store the match of . into $1 and the uppercase letter in $2, thus, you can use the following in the replace entry box:
$1_$2
I know you've accepted an answer, but when I ran it, I got From: _Integration_Name
Here's my idea;
(:\s)(.{1})([a-z]*)([A-Z]{1})
And use the following replace
$1$2$3_$4
I finaly did it like this:
Find: ([a-z])([A-Z])
Replace with: $1_$2

multi-line xml regex in sublime

I have a large logfile (+100 000 lines) in XML like so:
<container>
<request:getApples xml="...">
...
</request:getApples>
<request:getOranges xml="...">
...
</request:getOranges>
</container>
...
I want to extract the :getXXXX part to
getApples
getOranges
by doing a regex find & replace in Sublime Text 2.
Something like
Find: [^(request:)]*(.*) xml
Replace: $1\n
Any regex masters that can assist?
Correcting mart1n's answer and actually using ST2 and your sample input, I came up with the following:
First, CtrlA to select all. Then, CtrlH,
Search: .*?(get\w+) .*
Replace: $1
Replace All
Then,
Search: ^[^get].*$
Replace: nothing
Replace All
Finally,
Search: ^\n
Replace: nothing
Replace All
And you're left with:
getApples
getOranges
Not familiar with Sublime Text but you can do in two parts:
Find .*?\(get\w+\) .* and replace with \1. Now those get* strings are on separate lines with nothing else. All that remains is to remove the cruft.
So, many ways to do this. Easy one: find ^[^g][^e][^t].*$ and replace with nothing (an empty string).
Now you have your file that contains just the string you want and some empty lines, which (I hope) Sublime can get rid of with some delete-empty-lines function.
You can quickly throw all of the above in a macro and execute at will for any input following the same format ;-)
If you're willing to take the problem out of sublime text, you can use the dotall flag along with lazy matching to extract only the getXXX parts.
Replacing
.*?(get\w*) .*?
with
$1\n
should get you most of the way, only leaving a bit of easily removeable closing tags at the end of the file that I can't figure out at present.
You can check this solution here.
Maybe someone could take this and figure out a way to remove the extra closing tags.
Try this
Find what: :(\w+)>|.\s?
Replace with: $1
And if didn't work as intended, then let me know?

Notepad++ Regular Expressions find&remove

Need some help in Notepad++
Example how it looks at the moment
http://www.test.com/doc/rat.rar">rat.rar
http://www.test.com/down/ung.rar">ung.rar
http://www.test.com/read/add.rar">add.rar
......
How I want it (just remove after ">....rar)
http://www.test.com/doc/rat.rar
http://www.test.com/down/ung.rar
http://www.test.com/read/add.rar
Its a list about 1000 lines. So help would be nice
Use the following expression:
">[^.]+\.rar
Explanation:
"> # literal `"` followed by literal `>`
[^.]+ # any character that is not a `.`, repeated at least once
\. # literal `.` character
rar # literal string `rar`
Note: a couple of other answers pointed out that just ">.* will work. This is true, because Notepad++ doesn't appear to support multi-line regular expressions, even with [\s\S]+. Either way will work so it's personal preference. The regex I gave in this answer is very verbose and would reduce the likelihood of false positives. ">.*, on the other hand, is shorter.
In regexp mode , replace pattern ">.* with empty string.
">.*
Search for this and replace with nothing.
Your search string should be ">.+\.rar, and you can just blank out the replace box. This should do the job.
Also, check that you've got regex selected at the bottom of the replace box ;)
If you put this in find ".* and nothing in replace, that should do what you're looking for.
Remember to check that you've got regex selected at the bottom of the replace box.
Flick the "regular expression" radio button and then use this for your FIND:
">[a-z]+\.[a-z]+
Then just put empty space for your REPLACE and replace all.
Use -
Find What : (.*)">(.*)
Replace With : \1
And check Regular expression option at the bottom of the dialog.