Notepad++ regex replace Css #media - regex

I want to clean out everything between #media and }. Only after research I still cannot find how. I want to replace everything between the BOLD text below, including the BOLD text itself:
#media all and (max-width: 240px)
{
#toast-container>div
{
padding: 8px 8px 8px 50px;
width: 11em;
}
#toast-container .toast-close-button
{
right: -.2em;
top: -.2em;
};
}
#media (min-width: 768px)
{
.lead
{
font-size: 19.5px;
};
}
RESULT 1: #media }
RESULT 2: nothing

In Replace Dialog(Ctrl+H), enable Regular Expression and . matches newline
Then, search for:
#media.*?\};.*?\}
or
#media.*?(\};)\s*?\}
And replace for empty string... (this will remove all - including bold text)
Let me know if it works.

Related

How do I find everything beside regex in notepad++?

I want to find and mark all the code that doesn't fit my regex.
[^\}]*navbar[^\{]*\{[^\}]*[\}\s]*
Basically I want to mark everything beside red-highlighted part
my-regex-at-work.jpg
The code:
.pull-right>.dropdown-menu {
right: 0;
left: auto
}
.dropup .caret,
.navbar-fixed-bottom .dropdown .caret {
content: "";
border-top: 0;
border-bottom: 4px dashed;
border-bottom: 4px solid\9
}
.dropup .dropdown-menu,
.navbar-fixed-bottom .dropdown .dropdown-menu {
top: auto;
margin-bottom: 2px
bottom: 100%;
}
#media (min-width:768px) {
.navbar-right .dropdown-menu {
right: 0;
left: auto
}
.navbar-right .dropdown-menu-left {
right: auto;
left: 0
}
}
.btn-group,
.btn-group-vertical {
position: relative;
display: inline-block;
vertical-align: middle
}
My ideas do not work
[^[^\}]*navbar[^\{]*\{[^\}]*[\}\s]*]
[^[[^\}]*navbar[^\{]*\{[^\}]*[\}\s]*]]
it seems to be not possible with notepad++ because of backtracking, it could be done if regex engine supports control verbs:
[^\}]*navbar[^\{]*\{[^\}]*[\}\s]*(*SKIP)(?!)|.*
regex101
to tell to the engine to skip the matched part and fail, otherwise when engine fails to match it continues with input cursor on next character.
Append \K|.* to your regex:
[^\}]*navbar[^\{]*\{[^\}]*[\}\s]*\K|.*
Then hit Mark All button.

PHP - preg_replace and str_replace only replace only once inside foreach

I have a foreach that uses a Glob function in order to get some .htm files and finally put that content in one echo.
I Tried to replace one specific phrase located in all this .htm files with some data that I extract from the each .htm filename.
So,
$juzgado_radicado generates a number like ---> 004
$year_radicado generates a number like ---> 2015
$radicado_radicado generates a number like ---> 00911
That numbers are changing depending of foreach iteration getting a new file name.
But at the end I use preg_replace or str_replace in order to change a phrase inside the each .htm document but instead replacing the numbers in each iteration I only get the same result in all replaces. So I get something like "00911" in all .htm files and it have to be something like "00911" and "00577" por example.
I don't know why preg_replace and str_replace have this behaviour inside the foreach loop.
$directory2 = "/Applications/XAMPP/xamppfiles/htdocs/tecnojuridica/finalizado/";
$array_filename = glob($directory2 . "*.htm");
$string = '';
foreach($array_filename as $filename)
{
$xml_file = file_get_contents($filename, FILE_TEXT);
preg_match('/[0-9]{23}/', $filename, $match);
$radicado = $match[0];
// 002
$juzgado_radicado = substr($radicado, -14, 3);
// 2015
$year_radicado = substr($radicado, -11, 4);
// 00958
$radicado_radicado = substr($filename, -7, 5);
// Dom Object
$dom = new DOMDocument('1.0', 'utf-8');
$dom->xmlStandalone = true;
$dom->loadHTML($xml_file);
$content_node = $dom->getElementById("divrandom");
$mega = $dom->saveXML($content_node);
$string2 = preg_replace('/Datos del Proceso/', ' <font size="30"> <span style="background-color: #73A6FF; color: #fff; display: inline-block; padding: 3px 10px; font-weight: bold; border-radius: 5px;">' . $juzgado_radicado . '</span> <span style="background-color: #73A6FF; color: #fff; display: inline-block; padding: 3px 10px; font-weight: bold; border-radius: 5px;">' . $year_radicado . '</span> <span style="background-color: #73A6FF; color: #fff; display: inline-block; padding: 3px 10px; font-weight: bold; border-radius: 5px;">' . $radicado_radicado . ' </span> </font>', $mega);
$string.= $mega . ',';
}
echo utf8_decode($string2);
Thanks in advance.
I've found the Answer: You have to replace at star of the foreach loop not with Php Dom Document Objects.
If you tried to make replacement into Dom Objects it's possible that fails because str_replace of preg_replace can't make all iterations with different replace data.

What RegEx to select comma separated CSS selectors individually in SublimeText?

The other day I had to prefix every single CSS selector with another tag in order to increase its specificity. I though I'd flex my regex muscles a bit and I came up with this regex:
(^(((\s)+)?(?!.+: )(?!\.IE)[a-z.].+?)([,{]))
The plan was to replace the entire captured match with something like 'body $1'. There are a couple of negative lookaheads to avoid matching lines like background-color: property and .IE selectors
The screenshot describes the problem:
Here is some sample text:
/* line 18, ../scss/SplashPage/_page.scss */
* {
-webkit-tap-highlight-color: transparent;
}
/* line 35, ../scss/SplashPage/_page.scss */
body header {
*zoom: 1;
background: #eaeaea;
}
/* line 221, ../scss/MainStyle/_mixins.scss */
body header:before, body header:after {
display: table;
content: "";
}
/* line 224, ../scss/MainStyle/_mixins.scss */
body header:after {
clear: both;
}
body .utility-nav {
right:0
}
/* line 40, ../scss/SplashPage/_page.scss */
body .home-link {
text-indent: -99999px;
white-space: nowrap;
overflow: hidden;
background: transparent url("~/images/clipsal-logo2.png") center center no-repeat;
background-image: -webkit-linear-gradient(transparent, transparent), url("~/images/clipsal-logo.svg");
background-image: linear-gradient(transparent, transparent), url("~/images/clipsal-logo.svg");
background-position: top left;
background-repeat: no-repeat;
display: block;
float: left;
/*width: 120px;
height: 50px;
margin: 20px 0px 15px 25px;*/
background-size: 100% auto;
}
I think it's the start of line anchor (^) that needs do be dropped. Having said that I am not sure what the regex should be :)
Note: I understand that the problem could have easily been solved using SASS, but where's the fun in that!
Thanks

Regular Expression to get "left" only from style

This is my regular expression to get "left" and its value.
/(left\s*:\s*)(\d+)?(px)/
My problem is, it pulls padding-left and left.
vertical-align: top; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; display: list-item; width: 420px; position: absolute; top: 0px; left: 700px; height: 580px;
How can I get "left" and its value only?
I put a \s in front of left and that works... But I can't always guarantee there will be a space in front of left.
Thanks...
/(?:^|[^-])(left)\s*:\s*(\d+|auto)([a-z]{2})?/
Will fetch what you want with "left" in group 1, "700" in group 2 and "px" in group 3
(?:^|[^-]) = The start of the string, or a non-hyphen character
(left) = The word left (capture group 1)
\s*:\s* = A colon with optional space characters either side
(\d+|auto) = One or more numbers, or the string "auto" (capture group 2)
([a-z]{2})? = Two letters, e.g. "px" "em" "pt" (capture group 3 - optional)
Instead of using a RegEx, you could use a CSS parser. This is a more robust solution as a parser is better suited for this task.
An example using Python and cssutils
example.css
.myClass {
border: 1px solid black;
margin-left: 100px;
left: 700px;
padding-left: 50px;
}
get-left.py
#!/usr/bin/env python
import cssutils
sheet = cssutils.parseFile('example.css')
for rule in sheet:
if rule.type == rule.STYLE_RULE:
for prop in rule.style:
if prop.name == 'left':
print("{0}: {1}".format(prop.name, prop.value))
In this Python script, prop.name will return the name of the property and prop.value its value.
output
$ ./get-left.py
left: 700px

add class to each line of css with regex

I want to add a class at the start of each line of my css
I want
.more {
padding: 5px 10px 0;
}
#fbook {
display: inline;
float: left;
margin: 0 0 0 4px;
width: 320px;
}
to look like
.test .more {
padding: 5px 10px 0;
}
.test #fbook {
display: inline;
float: left;
margin: 0 0 0 4px;
width: 320px;
}
something like this.
^([A-Za-z0-9]+)$
would work if my css looked like this
more
{
padding: 5px 10px 0;
}
fbook
{
display: inline;
float: left;
margin: 0 0 0 4px;
width: 320px;
}
but I can't seem to get anything working when to find the . # {
Try this
$result = preg_replace('/(?i)^([.#a-z]+\{)$/m', '.test $1', $subject);
Explanation
"
(?im) # Match the remainder of the regex with the options: case insensitive (i); ^ and \$ match at line breaks (m)
^ # Assert position at the beginning of a line (at beginning of the string or after a line break character)
( # Match the regular expression below and capture its match into backreference number 1
[.#a-z] # Match a single character present in the list below
# One of the characters “.#”
# A character in the range between “a” and “z”
+ # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
\{ # Match the character “{” literally
)
\$ # Assert position at the end of a line (at the end of the string or before a line break character)
"
You can use a modern tool like LESS. Simply take your code, and wrap it with .test{ }:
.test {
.more {
padding: 5px 10px 0;
}
#fbook {
display: inline;
float: left;
margin: 0 0 0 4px;
width: 320px;
}
}
If you only need it one time, you can do it online: http://leafo.net/lessphp/#demo
If you want to do it from time to time, there are less libraries for many languages which you can use, and a client side JavaScript option.