Home  >  Article  >  Backend Development  >  Regular expression to match any character (including newline character)

Regular expression to match any character (including newline character)

高洛峰
高洛峰Original
2017-01-09 16:38:222398browse

Today I want to use regular expressions in Java to get any characters in a text. So I casually wrote the following matching rules:
(.*)
After running the result, I found that the text after the line break could not be obtained. So I checked the manual and found that in the regular expression, "." (dot symbol) matches all characters except the newline character "\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 matching rules of the regular expression as follows:
([.\n]*), of course, if it is written directly in the java program, it needs to be changed to ([.\\n]*)
As a result, when I ran the program again, I found that nothing could be retrieved. I couldn't figure it out, so I changed it to the following rules:
([.|\n]*) and ([\n.]*)
The result still didn't work, and I couldn't get any content. It seems that the dot symbol and the newline character are working hard~
Then I checked online. Although I didn’t find out where the problem was with the above rules, I found a solution. After a try, it can match the newline. Any characters including characters, the following are the correct regular expression matching rules:
([\s\S]*)
At the same time, you can also use "([\d\D]*)", " ([\w\W]*)" to represent.


In the text file, this expression can match all English
/[ -~]/
This expression can match all non-English (such as Chinese)
/[^ -~]/
/ is used in VI. You don’t need / in editplus or the program

For more articles on how to write regular expressions to match any character (including newlines), please visit Follow PHP Chinese website!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn