How to use a regular expression to match any character (including line breaks)

Source: Internet
Author: User

Http://parkmy.javaeye.com/blog/423440

Today, I want to use a regular expression in Java to obtain any character in a piece of text. Therefore, you can write the following matching rules at will:

(.*)

After the result is run, the text after the line break cannot be obtained. So I checked the manual and found that in the regular expression, "." (DOT symbol) matches all the characters except the linefeed "\ n. At the same time, there is another sentence in the manual: to match any character including '\ n', please use a pattern like' [. \ n. So I modified the regular expression matching rules as follows:

([. \ N] *), of course, if it is in JavaProgramTo change to [. \ n] *.

The result is that the program is run again and NO content can be obtained. I was puzzled and changed it to the following rules:

([. | \ N] *) and ([\ n.] *)

Note: In [], "." No longer indicates "matching characters other than line breaks", so you can use(. | \ N )*)To replace any character that contains the linefeed. Note that the parentheses are different from the brackets. Sometimes the linefeed must be represented by "\ r \ n ".(. | \ R \ n )*).Source: http://topic.csdn.net/t/20051010/18/4317410.html

 

The result still does not work, and nothing can be obtained. It seems that the dot and line break are busy ~

Then I checked the Internet. Although I didn't find out what the above rule was, I found a solution. After a try, I could indeed match any character including line breaks, the following are the correct Regular Expression matching rules:

([\ S] *)

You can also use "([\ D] *)" and "([\ W.

This article can be freely reproduced. Please retain the full text and indicate the source:
Reproduced from zhongzi said [http://www.wangzhongyuan.com/]
Link: http://www.wangzhongyuan.com/archives/640.html

In a text file, this expression can match all English letters.
/[-~] /
This expression can match all non-English characters (such as Chinese)
/[^-~] /

/Is Used in VI. You do not need it in editplus or program/

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.