How to add slightly similar content with regular expressions (regex)? - regex

I'm using Dreamweaver on a file that has about 30 instances the following:
'portfolio_bg' =>'#555555',
'portfolio_font'=>'#ffffff',
But, for each instance the hex codes are different. I want to add the following two lines underneath the above:
'product_bg' =>'#555555',
'product_font'=>'#ffffff',
where the hex codes in my two product lines will match the hex codes of the portfolio lines above it.
How do I accomplish his using regular expressions in Dreamweaver's Find and Replace?
Thanks in advance.

This works for me in EditPad Pro; it should work in Dreamweaver too.
Find:
'portfolio_bg'\s*=>\s*'(#[0-9A-Fa-f]+)',(\s+)'portfolio_font'\s*=>\s*'(#[0-9A-Fa-f]+)',\s*
Replace:
$&$2'product_bg' =>'$1',$2'product_font'=>'$3',$2
EDIT: corrected replacement string to use $& instead of $0.

For the first line, use the following RegEx replacement:
Find:
'portfolio_bg'[ \t]*=>[ \t]*'(#[0-9]{6})',
Replace:
'portfolio_bg' =>'\1',\n'product_bg' =>'\1',
For the second line,, use the following RegEx replacement:
Find:
'portfolio_font'[ \t]*=>[ \t]*'(#[0-9a-f]{6})',
Replace:
'portfolio_font' =>'\1',\n'product_font' =>'\1',

Related

In Notepad ++: Replacing some string with something else in all the lines containing another string

In the example below, is there any way to place a string like ("1one1") before {",} at the end of all lines which contain {ī}?
īn:"ZZin",
ín:"FFin",
ǐn:"QQin",
ìn:"TTin",
ie:"XXie",
iē:"TTie",
ié:"GGie",
Thanks
Using Notepad++ regex search for ^(.*ī.*)(",)$ and replace with \11one1\2.
You will need to use regex regex for notepad++.
so, mark "Regular Expression" in the final of Replace box.
in your fields to search:
find what: ī.[^"]"([A-Za-z0-9]*)
replace with: īn:"\11one1
i think it will do what you want. Let me know if it doesn't to edit the regex.

RegEx find & replace adding numbers infront of numbers leaving numbers as it is

I have this:
1200
3701
Trying to get this:
100ct1200
100ct3701
But got this:
100ct([0-9])([0-9])([0-9])([0-9])
Because I used regex in find & replace:
Find: ([0-9])([0-9])([0-9])([0-9])
Replace: 100ct([0-9])([0-9])([0-9])([0-9])
Actually I don't know how to express it to just add 100ct infront of the numbers and leave the rest as-it-is. Help? Thanks!
You need to use the following syntax
Find: (([0-9])([0-9])([0-9])([0-9]))
Replace: 100ct\1
Note the parentheses around the whole find expression: \1 refers to their content.
EDIT:
You can also use some shorthands for the Find expression:
Find: (\d{4})
It means "match 4 digits".
Your find and replace commands should be,
Find: ([0-9][0-9][0-9][0-9])
Replace: 100ct$1
$1 or \1 or #1 according to your tool.
Find: ([0-9][0-9][0-9][0-9])
Replace: 100ct$1 or 100ct\1 depending on the language (not sure what syntax Notepad++ uses).
Demo: http://regex101.com/r/cT1mJ8/1

notepad++ reg expressions to swap two values

i'm trying to swap latitude and longitude values in notepad++ with regular expressions. i tried to search some guide on the web but i didn't understand how to do. i have a file in which there are: "longitude,latitude" and i want to get: "latitude,longitude" in each row
Example (with two rows):
12.5164654350527,41.8919188281474
12.5164650441393,41.891919097598
becomes
41.8919188281474,12.5164654350527
41.891919097598,12.5164650441393
Which regular expression do i have to use?
Try with following regex:
(\d+\.\d+),(\d+\.\d+)
and replace it with:
\2,\1
Search for:
([0-9]+(\.[0-9]+)?),([0-9]+(\.[0-9]+)?)
Replace with:
\2,\1
This catches numbers like 1, 1.1 but not 1. or .5. My previous regexp ([0-9]+.?[0-9]*),([0-9]+.?[0-9]*) would allow for 1..
Make sure you place the cursor at the beginning of the file.
Hit CTRL+H.
Choose the Replace tab.
Select Regular Expression at the bottom.
Find: ([\d.]+),([\d.]+)
Replace: \2,\1
find what:
^([0-9]*\.[0-9]*),([0-9]*\.[0-9]*)$
replace with:
\2,\1
also, search mode should be set to regular expression
edit: escaped . as suggested in comments.

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++) Replace with regular expression: FixedText(xxx[yyy],whatever) to FixedText(yyy, xxx[yyy],whatever)

Sorry for not knowing the basics of regular expressions and asking this, but I couldn't get to it myself.
I need to replace all expressions PlayerTextDrawSetString(SSbank[playerid],strBank)
with PlayerTextDrawSetString(playerid,SSbank[playerid],strBank)
,
PlayerTextDrawSetString(SWant[someid],strWant)
with PlayerTextDrawSetString(someid,SWant[someid],strWant)
etc.
I can find such expressions with PlayerTextDrawSetString+\(+.+\[+.+\], but I can't replace them with that (\1, \2, \3 etc. return empty symbol).
I tried different search strings, but in all cases I get nothing on \1, \2, etc.
Could you please write the correct regex for me?
Thank you in advance for the help.
Try these expressions:
Search:
(PlayerTextDrawSetString\()(\w+\[)(\w+)
Replace:
\1\3,\2\3
The first two examples work.
Search Pattern 1:
SSbank\[([^\]]+)\]
Replacement Pattern 1:
\1,SSbank[\1]
Search Pattern 2:
SWant\[([^\]]+)\]
Replacement Pattern 2:
\1,SWant[\1]
To go all out if you have a lot of these similar patterns you can do this:
Total Replacement Search:
\((\w+)\[([^\]]+)\]
Total Replacement String (yes the first slash is needed... a bug maybe?):
\(\2,\1[\2]
Look behind is apparently broken.
I tried to give Notepad++ a shot with the positive look behind but the replacement fails even though the match happens. Here's the pattern:
(?<=\()(\w+)\[([^\]]+)\]
My attempted replacement (doesn't replace anything in Notepad++ v6.3.2):
\2,\1[\2]