I'm trying to use Notepadd++ to find all occurrences of width=xxx so I can change them to width="xxx"
as far as I have got is width=[^\n] which only selects width=x
If you need exactly 3 numbers, the following is tested in Notepad++:
width=\d\d\d[^\d]
Reading further into your requirement, you can use the tagging feature:
Find what: width=(\d\d\d)([^\d])
Replace with: width="\1"\2
Here, the (n) bracketed portions of the regex are stored (in sequence) as \1,\2,...\n which can be referred to in the replacement field.
As a regex engine, Notepad++ is poor. Here is a description of what's supported. Pretty basic.
Looking at the Notepad++ Regular Expression list there does not seem to support the {n} notation to match n characters, so \d{3} did not work.
However, what had worked for me and may be considered a hack was: \d\d\d
Tested in Notepad++ and has worked, for the Find field use (\d\d\d) and for the Replace filed use "\1"\2.
As Tao commented, as of version 6, Notepad++ supports PCRE.
So now You can write:
\d{1,5}
/(width=)(\d+?)/gim
Because you may want variable digits. Some widths may be 8, or 15, or 200, or whatever.
If you want to specify a range, you do it like this:
/(width=)(\d{1,3)/gim
where the 1 represents the lower limit and the 3 represents the upper.
I grouped both parts of the expression, so when you replace you can keep the first part and not blow it away.
Tried it: replace width=([0-9][0-9][0-9]) with width="\1" and worked fine... Of course might not be best syntax to do this but it works...
I would try this one: width=(\d{3,}), and check Regular expression, and also . matches newline
works for me on ver: 7.5.4
Related
I'm trying to write a regex that would detect if any combination of 4 non-whitespace characters existed between two strings. They will always be seperated by a comma. An example:
Labrador, Matador ---> this would match 'ador'.
Mississippi, Missing ---> This would match 'Miss' and 'issi'
Corporate, Corporation ---> This would match 'Corp' , 'orpo' , 'rpor' , 'pora' and 'orat'
It's been pretty hard to find something similar to this, and the closest I've found has said this is not possible in regex. It's definitely tricky, but I wanted to make sure that it was in fact not possible before looking for a different solution.
If it is impossible, would someone explain why?
For overlapping matches it is possible with a lookahead:
/(?=(\S{4}).*,.*\1)/
Note that there is one more issi possible in your second line example.
Test: https://regex101.com/r/rV3gN9/2
You can use this lookahead based regex:
(?=([a-zA-Z]{4})[a-zA-Z]*, *[a-zA-Z]*\1)
RegEx Demo
Though it will find issi twice since Mississippi has 2 instanced of issi.
This can be achieved with backreferences:
\w*([a-zA-z]{4})\w*, \w*\1\w*
See example: https://regex101.com/r/eW8hB7/1
I've got a big XML file and I should modify a tag.
Original:
<MyTag>13/19/59/70/68/32'</MyTag>'
What I want with regular expression:
<MyTag>13,19,59,70,68,32</MyTag>
That could be pretty easy if I'd got each time the same quantity of number but I could have 8 number or 5 or 6 or less.
How can I do that in one time?
As already pointed out in the comments, Notepad++'s regexes don't seem to be powerful enough to make that replace. In general, I don't think bare regex replacement isn't powerful enough for this replacement, you could at most get 13/19/59/70/68/32 in a capture group, and perform the / to , replace on that string by other means. That's why maybe I'd consider using another tool you are proficient in (perl, java, whatever) instead.
Using notepad++, I'd go for a normal replace first, to change all occurrences of '</MyTag>' to </MyTag>, and then a regex replace with this regular expression: (\d+)/. The replace should be \1,. Clicking on Replace all should replace all occurrences.
If you wanted to avoid replacing digits separated by / in other tags, maybe you could use this regular expression <MyTag>(.*)(\d+)/(.*)</MyTag> and replace it with <MyTag>\1\2,\3</MyTag>. This replace will have to be executed N times, so you might be interested in recording a macro or similar if you want to use it.
IT IS POSSIBLE TO DO IN ONE REGEXP.
Search for:
/([0-9]+)('(<){1}/(MyTag>){1}')?
Replace with:
,\1\3\4
When you address a regex capture, things can get tricky when digits follow the capture. In PCRE, I can write
${1}000
to substitute the capture of Group 1 followed by three zeroes.
Does anyone know the equivalent syntax in Dreamweaver replace operations, if any?
If we had a series of "A"s instead of zeroes, we could use:
$1AAAA
But these:
$10000
${1}0000
do not work.
I believe the regex flavor is ECMAScript. Just cannot find the information.
This may not be addressed in the syntax. If so, that would be good to know.
Thank you!
Edit: I should add that this is not matter of life and death as I have a number of grep tools at my fingertips. I would just like to know.
Dreamweaver's regular expression find and replace is supposed to be based on JavaScript's implementation of RegExp. You should be able to just use $1000 in the replacement text. However, like you've found, the replacement groups ($ + group number) are not properly recognized when the replacement text has digits immediately after the grouping token.
FWIW: I've logged a bug on this at http://adobe.ly/DWwish
I got a string like this:
PREFIX-('STRING WITH SPACES TO REPLACE')
and i need this:
PREFIX-('STRING_WITH_SPACES_TO_REPLACE')
I'm using Notepad++ for the Regex Search and Replace, but i'm shure every other Editor capable of regex replacements can do it to.
I'm using:
PREFIX-\('(.*)(\s)(.*)'\)
for search and
PREFIX-('\1_\3')
for replace
but that replaces only one space from the string.
The regex search feature in Notepad++ is very, very weak. The only way I can see to do this in NPP is to manually select the part of the text you want to work on, then do a standard find/replace with the In selection box checked.
Alternatively, you can run the document through an external script, or you can get a better editor. EditPad Pro has the best regex support I've ever seen in an editor. It's not free, but it's worth paying for. In EPP all I had to do was this:
search: ((?:PREFIX-\('|\G)[^\s']+)\s+
replace: $1_
EDIT: \G matches the position where the previous match ended, or the beginning of the input if there was no previous match. In other words, the first time you apply the regex, \G acts like \A. You can prevent that by adding a negative lookahead, like so:
((?:PREFIX-\('|(?!\A)\G)[^\s']+)\s+
If you want to prevent a match at the very beginning of the text no matter what it starts with, you can move the lookahead outside the group:
(?!\A)((?:PREFIX-\('|\G)[^\s']+)\s+
And, just in case you were wondering, a lookbehind will work just as well as a lookahead:
((?:PREFIX-\('|(?<!\A)\G)[^\s']+)\s+
You have to keep matching from the beggining of the string untill you can match no more.
find /(PREFIX-\('[^\s']*)\s([^']*'\))/
replace $1_$2
like: while (/(PREFIX-\('[^\s']*)\s([^']*'\))/$1_$2/) {}
How about using Replace all for about 20 times? Or until you're sure no string contains more spaces
Due to nature of regex, it's not possible to do this in one step by normal regular expression.
But if I be in your place, I do such replaces in several steps:
find such patterns and mark them with special character
(Like replacing STRING WITH SPACES TO REPLACE with #STRING WITH SPACES TO REPLACE#
Replace #([^#\s]*)\s to #\1_ server times.
Remove markers!
I studied a little the regex tool in Notepad++ because I didn't know their possibilities.
I conclude that they aren't powerful enough to do what you want.
Your are obliged to learn and use a programming language having a real regex capability. There are a number of them. Personnaly, I use Python. It would take 1 mn to do what you want with it
You'd have to run the replace several times for each space but this regex will work
/(?<=PREFIX-\(')([^\s]+)\s+/g
Replace with
\1_ or $1_
See it working at http://refiddle.com/10z
I'm a regexp newbie and I would like to know how to do a search and replace for the following case:
A file contains many occurrences of the following:
L1234_XL3.ext
and also many occurrences of:
L1234_XL3
I only want to find and replace L1234_XL3 occurrences with XL3 without affecting instances that have an extension.
I am using notepad++ to do the regular expression.
If Notepad++ supports lookaheads, you can simply use L1234_XL3(?!\.ext) for the search and "XL3" for the replacement.
EDIT: Looks like it doesn't support lookaheads after all. A pity; you'll have to do it the hard way without regexes (regexen?):
Replace L1234_XL3.ext with QQQ (or any other string that doesn't appear in the file)
Replace L1234_XL3 with XL3.
Replace QQQ with L1234_XL3.ext.
Step 1.
Change all occurences of L1234_XL3.ext to L-1-2-3-4_XL3.ext (for example)
Step 2.
Change all occurences of L1234_XL3 to XL3
Step 3.
Change all occurences of L-1-2-3-4_XL3.ext back to L1234_XL3.ext
As far as I understand Notepad++ 5.4.5 doesn't support positive lookahead