How to remove punctuation marks in text using regular expressions

Source: Internet
Author: User

In the recent retrieval work, the user needs to input the request and return the result. Because it is difficult to predict the user's input content, if no filtering is added, an exception may be thrown during the retrieval phase.

 

User input may have different styles. The most common is the input with various punctuation marks. How to filter?

I saw a post on the csdn Forum. After combining it, I got the following regular expression, which may help you solve the problem:


\ PP | \ PS

We use an example to demonstrate the regular expression usage:

Public static void main (string [] ARGs ){
String string = "test <>"! * (^) $ % ~! @ # $... & % ¥-+ = ,.,; ''" ": · 'Text ";
System. Out. println (string. replaceall ("\ PP | \ Ps ",""));
}

Output result:
Test text

 

 

The reference content is as follows:

 

Unicode encoding not only defines an encoding for a character, but also classifies it.

/PP the lower-case P indicates the Unicode attribute, which is used for the prefix of the Unicode positive expression.

Uppercase P indicates one of the seven character attributes of the Unicode Character Set: punctuation.

The other six are

L: letter;
M: Mark the symbol (generally it will not appear separately );
Z: delimiter (for example, space, line feed, etc );
S: Symbols (such as mathematical symbols and currency symbols );
N: Numbers (such as Arabic numerals and Roman numerals );
C: other characters

The above seven are attributes, and there are several sub-attributes under the seven attributes for further subdivision.

In Java, the Regular Expression data for Unicode is provided by Unicode organizations.

Unicode Regular Expression standard (all sub-attributes can be found)
Http://www.unicode.org/reports/tr18/

For the definition of UNICODE character attributes, you can see what attributes a character has.
Http://www.unicode.org/Public/UNIDATA/UnicodeData.txt

This text document contains a single character. The first column is Unicode encoding, the second column is the character name, and the third column is the Unicode attribute,
And other characters.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.