Regular expression with URL Encoded Strings - java

I have strings that contain URL encoding (%22) and other characters [!##$%^&*]. I need to use a RegEx to check if the string contains a character within that group but exclude the URL encoded quote (%22). I can't get the negative look ahead to work properly nor am I able to get an excluded string (or negation) working either. Can someone help? Here is code so far that doesn't work:
Pattern p = Pattern.compile("[!##$%^&*]"); //
String[] tokens = {"%22Hobo%22", "Shoe*", "Rail%6Road","Sugar"};
for (String string : tokens) {
Matcher m = p.matcher(string);
boolean b = m.find()
System.out.println(string + ": " + b);
}
The desired output should be false, true, true, false.

(?!%22)[!##$%^&*]
Try this.See demo.
https://regex101.com/r/mS3tQ7/16

export const uriParser = (x) =>
//replace/regex exclude-negated [set-of-tokens], doesn't work/parse for (%[A-Fa-f0-9]{2})+
//decodeURI() does the same I believe, but this will always return a string,
//without an error object
//a-z or A-Z includes underscore '_' but not space whitespace, nor (\r\n|\r|\n)+
x.replace(/(%[A-Fa-f0-9]{2})+[^a-zA-Z0-9-+ ]+/g, "_");
https://www.ietf.org/rfc/rfc3986.txt#:~:text=2.4.%20%20When%20to%20Encode%20or%20Decode%0A
for my purposes I make my uri link fragmens go thru (%[A-Fa-f0-9]{2})+ on mount so I use .replace("_"," ") for ui but uriParser() for outgoing links in ux to bypass redundancy as much as possible. The use case choice is between getting a string always & putting specifications for other characters before this. "Whyo you do not use a URLEncoder?" – Jens' comment on question

Related

Remove everything from String which is not on a allowlist using regex

Following regular expression removes each word from a string:
String regex = "\\b(operation|for the|am i|regex|mountain)\\b";
String sentence = "I am looking for the inverse operation by using regex";
String s = Pattern.compile(regex).matcher(sentence.toLowerCase()).replaceAll("");
System.out.println(s); // output: "i am looking inverse by using "
I am looking for the inverse operation by using regex. So following example should work.
The words "am i" and "mountain" just indicate that there can be much more words in the list. And also words with spaces can occur in the list.
String regex = "<yet to find>"; // contains words operation,for the,am i,regex,mountain
String sentence = "I am looking for the inverse operation by using regex";
String s = Pattern.compile(regex).matcher(sentence.toLowerCase()).replaceAll("");
System.out.println(s); // output: " for the operation regex"
Regards, Harris
Try the regex:
(?:(?!for the|operation|am i|mountain|regex).)*(for the|operation|am i|mountain|regex|$)
Replace the matches by contents of group 1 \1 or $1
Click for Demo
Click for Code
Explanation:
(?:(?!for the|operation|am i|mountain|regex).)* - matches 0+ occurrences of any character that is NOT followed by either for the or operation or am i or mountain or regex
(for the|operation|am i|mountain|regex|$) - matches either for the or operation or am i or mountain or regex or end of the string and captures it in group 1
To expand on Singh's answer in the comments, I'd add that hard-coding the regex for a set of words is not very portable. What if the words change? Are they just words or are they patterns? Can you isolate the part of code that will do this work and test it?
Assuming they're just words:
Define a whitelist
String[] whitelist = {
"operation",
"for",
"the",
"am i",
"regex",
"mountain"
};
Write a method for filtering the words so that only the whitelisted ones are allowed.
String sanitized(String raw, String[] whitelist) {
StringBuilder termsInOr = new StringBuilder();
termsInOr.append("|");
for (String word : whitelist) {
termsInOr.append(word);
}
String regex = ".*?\\b(" + termsInOr.substring(1) + ")\\b";
return Pattern.compile(regex, Pattern.MULTILINE)
.matcher(raw)
.replaceAll(subst);
}
This way the logic is isolated, you have two inputs - a whitelist and the raw string - and the sanitized output. It can be tested with assertions based on your expected output (test cases) if you have a different whitelist or raw string somewhere else in the code you can call the method with that whitelist / raw string to sanitize.

How not to match the first empty string in this regex?

(Disclaimer: the title of this question is probably too generic and not helpful to future readers having the same issue. Probably, it's just because I can't phrase it properly that I've not been able to find anything yet to solve my issue... I engage in modifying the title, or just close the question once someone will have helped me to figure out what the real problem is :) ).
High level description
I receive a string in input that contains two information of my interest:
A version name, which is 3.1.build and something else later
A build id, which is somenumbers-somenumbers-eitherwordsornumbers-somenumbers
I need to extract them separately.
More details about the inputs
I have an input which may come in 4 different ways:
Sample 1: v3.1.build.dev.12345.team 12345-12345-cici-12345 (the spaces in between are some \t first, and some whitespaces then).
Sample 2: v3.1.build.dev.12345.team 12345-12345-12345-12345 (this is very similar than the first example, except that in the second part, we only have numbers and -, no alphabetic characters).
Sample 3:
v3.1.build.dev.12345.team
12345-12345-cici-12345
(the above is very similar to sample 1, except that instead of \t and whitespaces, there's just a new line.
Sample 4:
v3.1.build.dev.12345.team
12345-12345-12345-12345
(same than above, with only digits and dashes in the second line).
Please note that in sample 3 and sample 4, there are some trailing spaces after both strings (not visible here).
To sum up, these are the 4 possible inputs:
String str1 = "v3.1.build.dev.12345.team\t\t\t\t\t 12345-12345-cici-12345";
String str2 = "v3.1.build.dev.12345.team\t\t\t\t\t 12345-12345-12345-12345";
String str3 = "v3.1.build.dev.12345.team \n12345-12345-cici-12345 ";
String str4 = "v3.1.build.dev.12345.team \n12345-12345-12345-12345 ";
My code currently
I have written the following code to extract the information I need (here reporting only relevant, please visit the fiddle link to have a complete and runnable example):
String versionPattern = "^.+[\\s]";
String buildIdPattern = "[\\s].+";
Pattern pVersion = Pattern.compile(versionPattern);
Pattern pBuildId = Pattern.compile(buildIdPattern);
for (String str : possibilities) {
Matcher mVersion = pVersion.matcher(str);
Matcher mBuildId = pBuildId.matcher(str);
while(mVersion.find()) {
System.out.println("Version found: \"" + mVersion.group(0).replaceAll("\\s", "") + "\"");
}
while (mBuildId.find()) {
System.out.println("Build-id found: \"" + mBuildId.group(0).replaceAll("\\s", "") + "\"");
}
}
The issue I'm facing
The above code works, pretty much. However, in the Sample 3 and Sample 4 (those where the build-id is separated by the version with a \n), I'm getting two matches: the first, is just a "", the second is the one I wish.
I don't feel this code is stable, and I think I'm doing something wrong with the regex pattern to match the build-id:
String buildIdPattern = "[\\s].+";
Does anyone have some ideas in order to exclude the first empty match on the build-id for sample 3 and 4, while keeping all the other matches?
Or some better way to write the regexs themselves (I'm open to improvements, not a big expert of regex)?
Based on your description it looks like your data is in form
NonWhiteSpaces whiteSpaces NonWhiteSpaces (optionalWhiteSpaces)
and you want to get only NonWhiteSpaces parts.
This can be achieved in numerous ways. One of them would be to trim() your string to get rid of potential trailing whitespaces and then split on the whitespaces (there should now only be in the middle of string). Something like
String[] arr = data.trim().split("\\s+");// \s also represents line separators like \n \r
String version = arr[0];
String buildID = arr[1];
(^v\w.+)\s+(\d+-\d+-\w+-\d+)\s*
It will capture 2 groups. One will capture the first section (v3.1.build.dev.12345.team), the second gets the last section (12345-12345-cici-12345)
It breaks down like: (^v\w.+) ensures that the string starts with a v, then captures all characters that are a number or letter (stopping on white space tabs etc.) \s+ matches any white space or tabs/newlines etc. as many times as it can. (\d+-\d+-\w+-\d+) this reads it in, ensuring that it conforms to your specified formatting. Note that this will still read in the dashes, making it easier for you to split the string after to get the information you need. If you want you could even make these their own capture groups making it even easier to get your info.
Then it ends with \s* just to make sure it doesn't get messed up by trailing white space. It uses * instead of + because we don't want it to break if there's no trailing white space.
I think this would be strong for production (aside from the fact that the strings cannot begin with any white-space - which is fixable, but I wasn't sure if it's what you're going for).
public class Other {
static String patternStr = "^([\\S]{1,})([\\s]{1,})(.*)";
static String str1 = "v3.1.build.dev.12345.team\t\t\t\t\t 12345-12345-cici-12345";
static String str2 = "v3.1.build.dev.12345.team\t\t\t\t\t 12345-12345-12345-12345";
static String str3 = "v3.1.build.dev.12345.team \n12345-12345-cici-12345 ";
static String str4 = "v3.1.build.dev.12345.team \n12345-12345-12345-12345 ";
static Pattern pattern = Pattern.compile(patternStr);
public static void main(String[] args) {
List<String> possibilities = Arrays.asList(str1, str2, str3, str4);
for (String str : possibilities) {
Matcher matcher = pattern.matcher(str);
if (matcher.find()) {
System.out.println("Version found: \"" + matcher.group(1).replaceAll("\\s", "") + "\"");
System.out.println("Some whitespace found: \"" + matcher.group(2).replaceAll("\\s", "") + "\"");
System.out.println("Build-id found: \"" + matcher.group(3).replaceAll("\\s", "") + "\"");
} else {
System.out.println("Pattern NOT found");
}
System.out.println();
}
}
}
Imo, it looks very similar to your original code. In case the regex doesn't look familiar to you, I'll explain what's going on.
Capital S in [\\S] basically means match everything except for [\\s]. .+ worked well in your case, but all it is really saying is match anything that isn't empty - even a whitespace. This is not necessarily bad, but would be troublesome if you ever had to modify the regex.
{1,} simple means one or more occurrences. {1,2}, to give another example, would be 1 or 2 occurrences. FYI, + usually means 0 or 1 occurrences (maybe not in Java) and * means one or more occurrences.
The parentheses denote groups. The entire match is group 0. When you add parentheses, the order from left to right represent group 1 .. group N. So what I did was combine your patterns using groups, separated by one or more occurrences of whitespace. (.*) is used for group 2, since that group can have both whitespace and non-whitespace, as long as it doesn't begin with whitespace.
If you have any questions feel free to ask. For the record, your current code is fine if you just add '+' to the buildId pattern: [\\s]+.+.
Without that, your regex is saying: match the whitespace that is followed by no characters or a single character. Since all of your whitespace is followed by more whitespace, you matching just a single whitespace.
TLDR;
Use the pattern ^(v\\S+)\\s+(\\S+), where the capture-groups capture the version and build respectively, here's the complete snippet:
String unitPattern ="^(v\\S+)\\s+(\\S+)";
Pattern pattern = Pattern.compile(unitPattern);
for (String str : possibilities) {
System.out.println("Analyzing \"" + str + "\"");
Matcher matcher = pattern.matcher(str);
while(matcher.find()) {
System.out.println("Version found: \"" + matcher.group(1) + "\"");
System.out.println("Build-id found: \"" + matcher.group(2) + "\"");
}
}
Fiddle to try it.
Nitty Gritties
Reason for the empty lines in the output
It's because of how the Matcher class interprets the .; The . DOES NOT match newlines, it stops matching just before the \n. For that you need to add the flag Pattern.DOTALL using Pattern.compile(String pattern, int flags).
An attempt
But even with Pattern.DOTALL, you'll still not be able to match, because of the way you have defined the pattern. A better approach is to match the full build and version as a unit and then extract the necessary parts.
^(v\\S+)\\s+(\\S+)
This does trick where :
^(v\\S+) defines the starting of the unit and also captures version information
\\s+ matches the tabs, new line, spaces etc
(\\S+) captures the final contiguous build id

Get a specific word out of a String with regex

I have an String
String string = "-minY:50 -maxY:100 -minVein:8 -maxVein:10 -meta:0 perChunk:5;";
And I want to somehow get the -meta:0 out of it with regex (replace everything except -meta:0), I made an regex which deletes -meta:0 but I can't make it delete everything except -meta:0
I tried using some other regex but it was ignoring whole line when I had -meta:[0-9] in it, and like you can see I have one line for everything.
This is how it has been deleting -meta:0 from the String:
String meta = string.replaceAll("( -meta:[0-9])", "");
System.out.println(meta);
I just somehow want to reverse that and delete everything except -meta:[0-9]
I couldn't find anything on the page about my issue because everything was ignoring whole line after it found the word, so sorry if there's something similar to this.
You should be capturing your match in a captured group and use it's reference in replacement as:
String meta = string.replaceAll("^.*(-meta:\\d+).*$", "$1");
System.out.println(meta);
//=> "-meta:0"
RegEx Demo
As I understand your requirement you want to :
a) you want to extract meta* from the string
b) replace everything else with ""
You could do something like :
String string = "-minY:50 -maxY:100 -minVein:8 -maxVein:10 -meta:0 perChunk:5;";
Pattern p = Pattern.compile(".*(-meta:[0-9]).*");
Matcher m = p.matcher(string);
if ( m.find() )
{
string = string.replaceAll(m.group(0),m.group(1));
System.out.println("After removal of meta* : " + string);
}
What this code does is it finds meta:[0-9] and retains it and removes other found groups

contains() method without prefix and suffix in Java

I'm stuck on a (simple, I think) String validation. I use the following method to get a text from an EditText which can have 420 chars and must to contain a specific word (whatever its place):
if(edittext.getText().toString().toLowerCase().contains(stringToHave)) { }
// stringToHave = the specific String
However, I want to improve this condition. For example, you have this:
String a = "This not a MetaStackOverflow question";
String b = "stackoverflow";
And you want to know if a contains b regardless is case sensitive, then, you do as follows:
if(a.toLowerCase().contains(b)) { }
This condition is true because indeed a contains StackOverflow. However, a doesn't contain exactly b, there is a prefix as Meta. And this is not exactly the same word.. I tried to find a way on SO and on other Java websites without result.
How can I improve contains() method to find only the exact String without prefix or suffix? Should I use another method (as containsOnly(), I already tried it but it seems that is undefined for String, it's to check if it contains numeric/alphabetic/etc. chars)?
EDIT:
Add a "two spaces" verification is very attractive and ingenious. However, if the specific text is at the end of the sentence and we add a . this will not work, isn't it?
a = "This is great. This is on StackOverflow." // false because we have "."
You can use regex here. Matcher class has a find() method, which searches for a pattern in a string. This will work:
Matcher matcher = Pattern.compile("(?i)\\b" + Pattern.quote(b) + "\\b").matcher(a);
if (matcher.find()) {
// contains
}
(?i) is case-insensitive embedded flag. Pattern.quote() is used to escape the regex meta-characters if any in the search string.
Use space before and after the pattern what you seek:
if(a.toLowerCase().contains(" " + b + " ")) { }
You can use a case insensitive Pattern with word boundaries \\b to do this:
String a = "This not a MetaStackOverflow question";
String b = "stackoverflow";
Pattern p = Pattern.compile("\\b" + b + "\\b", Pattern.CASE_INSENSITIVE);
if (p.matcher(a).find()) { }

Split by space but not newline

I am trying to convert all links in a given string to clickable a tags using the following code :
String [] parts = comment.split("\\s");
String newComment=null;
for( String item : parts ) try {
URL url = new URL(item);
// If possible then replace with anchor...
if(newComment==null){
newComment=""+ url + " ";
}else{
newComment=newComment+""+ url + " ";
}
} catch (MalformedURLException e) {
// If there was an URL that was not it!...
if(newComment==null){
newComment = item+" ";
}else{
newComment = newComment+item+" ";
}
}
It works fine for
Hi there, click here http://www.google.com ok?
converting it to
Hi there, click here http://www.google.com ok?
But when the string is this :
Hi there, click
here http://www.google.com
ok?
its still converting it to :
Hi there, click here http://www.google.com ok?
Whereas I want the final result to be :
Hi there, click
here http://www.google.com
ok?
I think its including the newline character also while making the split.
How do I preserve the newline character in this case ?
I would suggest a different approach:
String noNewLines = "Hi there, click here http://www.google.com ok?";
String newLines = "Hi there, \r\nclick here \nhttp://www.google.com ok?";
// This is a String format with two String variables.
// They will be replaced with the desired values once the "format" method is called.
String replacementFormat = "%s";
// The first round brackets define a group with anything starting with
// "http(s)". The second round brackets delimit that group by a lookforward reference
// to whitespace.
String pattern = "(http(s)?://.+?)(?=\\s)";
noNewLines = noNewLines.replaceAll(
pattern,
// The "$1" literals are group back-references.
// In our instance, they reference the group enclosed between the first
// round brackets in the "pattern" String.
new Formatter().format(replacementFormat, "$1", "$1")
.toString()
);
System.out.println(noNewLines);
System.out.println();
newLines = newLines.replaceAll(
pattern,
new Formatter().format(replacementFormat, "$1", "$1")
.toString()
);
System.out.println(newLines);
Output:
Hi there, click here http://www.google.com ok?
Hi there,
click here
http://www.google.com ok?
This will replace all your http(s) links to an anchor reference, whether or not you have newlines (windows or *nix) in your text.
Edit
For best coding practices you should set the replacementFormat and pattern variables as constants (so, final static String REPLACEMENT_FORMAT and so on).
Edit II
Actually grouping the URl pattern isn't really necessary, as the whitespace lookahead is sufficient. But well, I'm leaving it as is, it works.
You could just use
String [] parts = comment.split("\\ ");
instead of
String [] parts = comment.split("\\s");
as eldris said, "\s" is for every white-space character, so "\ ", for just the space character itself should do for you.
I would suggest following solution to your problem:
First split by new line character
For each line do processing that you have mentioned above
Add all processed lines
That ways new line character will be retained and also you will be able to do in each line what you are currently doing.
Hope this helps.
Cheers !!

Categories