Reputation: 2017
I'm trying to write a regex that matches any semicolon symbol ;
that doesn't start with !important
or !important
For instance, if I have the following string:
.myclass {
width: 400px;
color: #333;
margin: 20px !important;
padding: 20px !important ;
top: 1px;
}
I want to match those lines:
width: 400px;
color: #333;
top: 1px;
So I can then run replace on them and add the !important
attribute to them.
How should I write the regex that would match this?
Upvotes: 0
Views: 128
Reputation: 19397
^(?!.*!important\s*;).*?(;)\s*$
^^^^^^^^^^^^^^^^ ^
(1) (2)
!important
followed by zero or more whitespace characters followed by a semicolon.See the live demo.
Upvotes: 0
Reputation: 20163
If this is entirely in one string variable
.myclass {
width: 400px;
color: #333;
margin: 20px !important;
padding: 20px !important ;
top: 1px;
}
then you can split it on the new-line:
String[] lines = input.split(System.getProperty("line.separator", "\r\n"));
Then, skipping the first and last elements (which contain the curly-braces), only get lines that do not match "!important ?;"
Matcher importantMtchr = Pattern.compile("!important ?;").matcher("");
for(int i = 1; i < lines.length - 1; i++) {
String line = lines[i];
if(!importantMtchr.reset(line).find()) {
System.out.println(line);
}
}
Full code:
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class NotExclamationImportantLines {
public static final String LINE_SEP = System.getProperty("line.separator", "\r\n");
public static final void main(String[] ignored) {
String input = new StringBuilder().
append("myclass {" ).append(LINE_SEP).
append("width: 400px;" ).append(LINE_SEP).
append("color: #333;" ).append(LINE_SEP).
append("margin: 20px !important;" ).append(LINE_SEP).
append("padding: 20px !important ;").append(LINE_SEP).
append("top: 1px;" ).append(LINE_SEP).
append("}" ).toString();
//Split on the newline
String[] lines = input.split(LINE_SEP);
//Skip over the first and last elements, and only
//print out those that don't contain the regular
//expression `"important! ?"
Matcher importantMtchr = Pattern.compile("!important ?;").matcher("");
for(int i = 1; i < lines.length - 1; i++) {
String line = lines[i];
if(!importantMtchr.reset(line).find()) {
System.out.println(line);
}
}
}
}
Output:
width: 400px;
color: #333;
top: 1px;
Upvotes: 0
Reputation: 4010
Try using this one: (?!.*!important).*;
.
Breaking it down into smaller pieces we are using a negative lookahead (?!<pattern>)
to say we want to match where there is NOT the match later in the string. After that, we just need to look for any chars up until we see a closing ;
. The way the negative lookahead is setup, if the line is ending in the ;
and there is a match to !important
at all it will fail, no matter how many spaces are inbetween. Since CSS can have spaces, this handles a lot more cases you could see other then 0 or 1 spaces.
If you wanted it to be EXACTLY like the original post where you are checking for zero or one space after !important
but before ;
, you can change the lookahead to include \s?;
, after !important
of course. This is checking for ANY whitespace, zero or one of, followed directly by the ;
.
Upvotes: 4
Reputation: 2001
This one worked for me at a Regex tester:
.+[^((?!important)\s];
The regex matches any number of characters (.+
) except the ones with !important in it ([^!important]
).
Upvotes: 0