Java regex to replace whitespaces in css class string with dots - java

Given a class attribute of an HTML element, I want to generate a String suitable for css selector.
For instance, the element's class attribute value:
' foo bar baz '
OR
'foo bar baz '
Should both become a css selector:
'.foo.bar.baz'
Right now, I'm using:
String classCssSelector = "." + classProp.trim().replaceAll("\\s+", ".");
But I want to get rid of the leading hardcoded "." + and the trim() and make it all one replaceAll call.
EDIT:
I used the regex provided in #anubhava's answer, but then wanted it to also match an already 'dotted' class like this:
' foo bar baz '
The following regex works for both cases:
^(?!\.)| +(?= *\S)

You can use this regex in replaceAll:
^ *| +(?= *\S)
RegEx Demo
Code:
String classCssSelector = classProp.replaceAll("^ *| +(?= *\\S)", ".");
Explanation:
^ - Match beginning
^ * - Match 0 or more spaces at start
| - Alternation in regex
| + - Match 1 more spaces after `|`
(?= *\\S) - Lookahead to make sure there is at least one non-space character after matching
space in previous match

Related

Regex to replace the character after pattern match

For my Java app, i have a string that looks like:
value1: *sample", test
test: "test"
newtest: *newtest"
I need to match the character " when the string starts with *.
Tried the regex:
"(?!.*")
But this selected all the " in the input.
Was planning to replaceAll(regex, "") to remove the character.
Desired Output:
value1: *sample, test
test: "test"
newtest: *newtest
How do i get this output?
You can use
.replaceAll("(\\*\\w+)\"", "$1")
Details:
(\*\w+) - Group 1 ($1 refers to the text captured in this group)
" - a " char (just matched, not captured, so eventually removed).

String replacement when regex reverse group is null in java

I want to convert a software version number into a github tag name by regular expression.
For example, the version of ognl is usually 3.2.1. What I want is the tag name OGNL_3_2_1
So we can use String::replaceAll(String regex, String replacement) method like this
"3.2.1".replaceAll("(\d+).(\d+).(\d+)", "OGNL_$1_$2_$3")
And we can get the tag name OGNL_3_2_1 easily.
But when it comes to 3.2, I want the regex still working so I change it into (\d+).(\d+)(?:.(\d+))?.
Execute the code again, what I get is OGNL_3_2_ rather than OGNL_3_2. The underline _ at the tail is not what I want. It is resulted by the null group for $3
So how can I write a suitable replacement to solve this case?
When the group for $3 is null, the underline _ should disappear
Thanks for your help !!!
You can make the last . + digits part optional by enclosing it with an optional non-capturing group and use a lambda as a replacement argument with Matcher.replaceAll in the latest Java versions:
String regex = "(\\d+)\\.(\\d+)(?:\\.(\\d+))?";
Pattern p = Pattern.compile(regex);
String s="3.2.1";
Matcher m = p.matcher(s);
String result = m.replaceAll(x ->
x.group(3) != null ? "OGNL_" + x.group(1) + "_" + x.group(2) + "_" + x.group(3) :
"OGNL_" + x.group(1) + "_" + x.group(2) );
System.out.println(result);
See the Java demo.
The (\d+)\.(\d+)(?:\.(\d+))? pattern (note that literal . are escaped) matches and captures into Group 1 any one or more digits, then matches a dot, then captures one or more digits into Group 2 and then optionally matches a dot and digits (captured into Group 3). If Group 3 is not null, add the _ and Group 3 value, else, omit this part when building the final replacement value.

How to get regEx for \" string in a given string

We need to give like this :I\"THIS IS TEST[1]\" for the data : I\"THIS IS TEST[1]\" - to be inserted into the Redshift.
But the above thing has to be done only if we have \" in the given string , So i am searching how to identify the \" in a given string, tried with regEx , but no luck. Can you please suggest here how to check if the string has \" so that if a match is found then i will replaceAll ("\"" , "\\"");
Thank you!!!
In order to check whether a String contains the string \" you must firstly match the character \ literally by adding \ to your Regular Expression and then match the " character by adding \".
The end Regular Expression is \\" and this is tested to match your provided string on https://regex101.com/
Quick test:
String regex = "\\\"";
Pattern pattern = Pattern.compile(regex);
String toTest = "hi there \". Do I match?";
boolean found = pattern.matcher(toTest).find();
System.out.println("Boolean value for whether pattern matches toTest: " + found);
Output: "Boolean value for whether pattern matches toTest: true"
After found is true, simply run your replaceAll command like so:
String#replaceAll(regex, "new value");
Note: \" is just a ". \\" will match \". \\\" will match \". Every time you want to match an additional \, just add \\ to your Regular Expression - Alternatively, if you want to match an additional " add \" to your Regular Expression

Weird password check matching using regex in Java

I'm trying to check a password with the following constraint:
at least 9 characters
at least 1 upper case
at least 1 lower case
at least 1 special character into the following list:
~ ! # # $ % ^ & * ( ) _ - + = { } [ ] | : ; " ' < > , . ?
no accentuated letter
Here's the code I wrote:
Pattern pattern = Pattern.compile(
"(?!.*[âêôûÄéÆÇàèÊùÌÍÎÏÐîÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ€£])"
+ "(?=.*\\d)"
+ "(?=.*[a-z])"
+ "(?=.*[A-Z])"
+ "(?=.*[`~!##$%^&*()_\\-+={}\\[\\]\\\\|:;\"'<>,.?/])"
+ ".{9,}");
Matcher matcher = pattern.matcher(myNewPassword);
if (matcher.matches()) {
//do what you've got to do when you
}
The issue is that some characters like € or £ doesn't make the password wrong.
I don't understand why this is working that way since I explicitly exclude € and £ from the authorized list.
Rather than trying to disallow those non-ascii characters why not makes your regex accept only ASCII characters like this:
Pattern pattern = Pattern.compile(
"(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*\\p{Print})\\p{ASCII}{9,})");
Also see use of \p{Print} instead of the big character class. I believe that would be suffice for you.
Check Javadoc for more details
This just allows printable Ascii. Note that it allows space character, but you could disallow space by setting \x21 instead.
Edit - I didn't see a number in the requirement, saw it in your regex, but wasn't sure.
# "^(?=.*[A-Z])(?=.*[a-z])(?=.*[`~!##$%^&*()_\\-+={}\\[\\]|:;\"'<>,.?])[\\x20-\\x7E]{9,}$"
^
(?= .* [A-Z] )
(?= .* [a-z] )
(?= .* [`~!##$%^&*()_\-+={}\[\]|:;"'<>,.?] )
[\x20-\x7E]{9,}
$

Making group constraints in Regex in android

So I have this String:
String articleContent = "dfgh{jdf%g{%qf234ad%22!#$56a%}vzsams{%3%45%}678456{78";
I want to remove everything between {% %}
So result would be something like :
dfgh{jdf%gvzsams678456{78
I tried this:
String regex = "[{%][^[%}]&&\\p{Graph}]*[%}]";
String abc = articleContent.replaceAll(regex, "");
But what I get is:
dfghfgqf234ad}vzsams3}678456{78
What I suppose I'm doing wrong is not able to make a group of "{%" instead of [{%] which is like an or condition { or % .
Any suggestions?
EDIT 1:
The string that I have taken is just for an example. It can have any special characters in between {% and %} not only ! and %
You can do it with this pattern:
String regex = "\\{%(?>[^%]++|%(?!}))*%}";
explanations:
The goal of this pattern is to reduce at the minimum the number of backtracks:
\\{% # { need to be escaped
(?> # open an atomic group *
[^%]++ # all characters but %, one or more times (possessive *)
| # OR
%(?!}) # % not followed by } (<-no need to escape)
)* # close the atomic group, repeat zero or more times
%}
(* more informations about possessive quantifiers and atomic groups)
Try this way
String abc = articleContent.replaceAll("\\{%.*?%}", "")
Since { is special characters you need to escape it. You can do this with \\{ or [{].
Now to match all characters between {% and %} you can use {%.*%}, but * quantifier is greedy so it will match maximal possible substring between first {% and last %}. To make it match minimal substring we need to add ? after * making it reluctant.
You can find more info about quantifiers here.

Categories

Resources