How to escape # symbol in url? - java

I had # symbol to pass as parameter in my URL. But it is discarding all the parameter values after #. Kindly suggest me solution. Following is my url
GetConnectiont?
customerID=customer1&activenode=Sv50&parent=server&frame=imHealthCheckFrame&
connectedFrom=healthCheck&username=root&password=anil#1234&fromAskUsrPwd=askPassword
Thanks in advance

Per the answer found here: How to escape Hash character in URL
Replace # with %23.
You can find a list of all the reserved characters here: Percent-encoding.
A word on JavaScript encoding
encodeURI(str)
"Assumes that the URI is a complete URI, so does not encode reserved characters that have special meaning in the URI. encodeURI replaces all characters except the following with the appropriate UTF-8 escape sequences:" [1]
encodeURIComponent(str)
"Escapes all characters except the following: alphabetic, decimal digits, - _ . ! ~ * ' ( )" [2]
In your case, you would use encodeURIComponent(), only on your query string, to escape any and all reserved chracters.
! %21
# %23
$ %24
& %26
' %27
( %28
) %29
* %2A
+ %2B
, %2C
/ %2F
: %3A
; %3B
= %3D
? %3F
# %40
[ %5B
] %5D

You would need to encode the parameters that may contain # and then decode back the parameter in server side code.
You can use many resources in the internet to URL-encode strings, like this. In your case, anil#1234 becomes anil%231234.

You need to encode the value(if you are using string concatenation to create the url). in javascript you can use encodeURIComponent() like
encodeURIComponent('anil#1234');//or your variable here

Related

Spring boot Request Mapping URI Patterns

There is a request mapping like this:
#DeleteMapping(value = "/{version:.+}")
I not sure what is the .+ does, but from what i know, this delete mapping can accept a value and match to path variable version, something like:
DELETE
/abc
Value abc will map to path variable version
Why the .+ is needed?
Edited Question:
What is the difference with just /{version}, is there any special case that requires .+?
You can find details or URL matching on this link
URL matching
REGEX: .+ means one or more.
‘*’ Matches 0 or More Characters
'+' Matches 1 or More.
#DeleteMapping(value = "/{version:.+}")
.+ means "one or more of any characters" - thats standard regex/
version: means - put that match in path variable named version.

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,}
$

Java - Capture optional field with regexp?

I've a regex that correctly captures values from the result of a string.
regex is look like;
intGetHatSaatRenk_v22=anyType{SiraNo=(.*?); HatKodu=(.*?) ; GunTipi=(.*?); Gidis=(.*?); ? };
But the problem is the source is like;
intGetHatSaatRenk_v22=anyType{SiraNo=54; HatKodu=502 ; GunTipi=C; Gidis=12:00; RenkGidis=0000FF; };
intGetHatSaatRenk_v22=anyType{SiraNo=55; HatKodu=502 ; GunTipi=C; Gidis=12:07; }; intGetHatSaatRenk_v22=anyType{SiraNo=56; HatKodu=502 ; GunTipi=C; Gidis=12:14; };
as you can see there is an optional field that named RenkGidis, how can i get the value from RenkGidis if it's not null?
with the regex code that i wrote above, i can get if RenkGidis exists in group(4) like 12:00; RenkGidis=0000FF but group(4) must be only 12:00.
I hope that I could explain my problem.
Might want to make the last group optional:
intGetHatSaatRenk_v22=anyType\{SiraNo=([^;\s]*);\s+HatKodu=([^;\s]*)\s*;\s+GunTipi=([^;\s]*);\s+Gidis=([^;\s]*);(?:\s+RenkGidis=([^;\s]*);)?
As a Java string:
"intGetHatSaatRenk_v22=anyType\\{SiraNo=([^;\\s]*);\\s+HatKodu=([^;\\s]*)\\s*;\\s+GunTipi=([^;\\s]*);\\s+Gidis=([^;\\s]*);(?:\\s+RenkGidis=([^;\\s]*);)?"
At the last group ( ?: prevents the group to be captured into output. ( inside ) catpured as usual.
Also changed .*? to [^;\s]* (negation of [;\s] -> any characters, that are no white-space or ;)
As Alan mentioned in the comments, for not getting a null match for the optional part, e.g. just make RenkGidis optional and wrap the value in an alternation with nothing: ([^;\s]*;|)
intGetHatSaatRenk_v22=anyType\{SiraNo=([^;\s]*);\s+HatKodu=([^;\s]*)\s*;\s+GunTipi=([^;\s]*);\s+Gidis=([^;\s]*);(?:\s+RenkGidis=)?([^;\s]*|)
As a Java string:
"intGetHatSaatRenk_v22=anyType\\{SiraNo=([^;\\s]*);\\s+HatKodu=([^;\\s]*)\\s*;\\s+GunTipi=([^;\\s]*);\\s+Gidis=([^;\\s]*);(?:\\s+RenkGidis=)?([^;\\s]*|)"
The regex could look like this
intGetHatSaatRenk_v22=anyType\{SiraNo=(.*?); HatKodu=(.*?) ; GunTipi=(.*?); Gidis=(.*?);( RenkGidis=.*?;\s*|\s*)\};
Group 5 will then be either " RenkGidis=0000FF;" or " ". You can then use a second regex to get 0000FF.

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.

Special chars in JAVA

สวัสดี Mr.Java Sp'e c'i'a'l'' '
I tried to parse the String using below code but I could't make
simply it shows the wrong value.
String s = "สวัสดี Mr.Java Sp'e c'i'a'l'' '"";
s = s.replaceAll("'", "&apos;");
//s = s.replaceAll("'", "''");
StringEscapeUtils.escapeHtml(s);
I am trying to get from JSP and save in SQL Server DB and show using JSP and update.
But some times in JSP it shows the converted &apos in jsp as it is instead of Special
Chars.
Very Simple is Here I have shown this String(สวัสดี Mr.Java Sp'e c'i'a'l'' ') in StackOverflow they
save in their DB and Shows and allows me to update this is what I
wanted.
OK. So lets look at what your code does:
// line 1
String s = "สวัสดี Mr.Java Sp'e c'i'a'l'' '";
We have a String with various international characters in it ... and some "'" characters.
// line 2
s = s.replaceAll("'", "&apos;");
Assuming that those are really "'" characters characters, we will replace all instances of "'" with an XML / HTML character entity giving us:
"สวัสดี Mr.Java Sp&apos;e c&apos;i&apos;a&apos;l&apos;&apos; &apos;"
And so ...
// line 3
s = StringEscapeUtils.escapeHtml(s);
This replaces any active HTML / XML characters with character references. This includes the ampersand characters "&" that you previously inserted. The result is this:
"&#xxxx;&#xxxx;&#xxxx;&#xxxx; Mr.Java Sp&apos;e
c&apos;i&apos;a&apos;l&apos;&apos; &apos;"
(The &#xxxx; numeric character references encode those Thai (?) characters.)
When you embed that in an HTML document and display it, you will see "สวัสดี Mr.Java Sp&apos;e c&apos;i&apos;a&apos;l&apos;&apos; &apos;"
See what has happened? You have HTML escaped your HTML escaped apostrophies!!
So what do you really need to do?
There is no need replace apostrophes with &apos;. Apostrophes are legal in HTML text.
There should be no need to add HTML escapes so that you can store text in a database:
Any modern database will allow you to store Unicode strings without any special encoding.
If you are trying to prevent the database's SQL parser getting confused by quotes in the text you are storing, you are doing it the wrong way. The right way to do this is to use a PreparedStatement, add parameter placeholders to the query, and use the PreparedStatement.setXxx methods to provide the parameter values. The execute (or whatever) will take care of any SQL escaping that needs to be done.

Categories

Resources