In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what Java is convenient regular expression", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's train of thought slowly in depth, together to study and learn "what Java convenient regular expression" bar!
Find sentences that contain specific words
Suppose we want to match all the sentences in the text that contain specific words. Because you need to display these sentences in the search results, or you want to delete them from the text. The regular expression / [^!?] *\ bword\ b [^.?] *. / gi can help us do this. As follows:
Const str = "The apple tree originated in Central Asia. It is cultivated worldwide. Apple matures in late summer or autumn." / / find the sentence str.match (/ [^.!?] *\ bapple\ b [^.?] * / gi) / / output the result / / = > ["The apple tree originated in Central Asia.", "Apple matures in late summer or autumn."]
Next, let's look at the meaning of this regular expression:
[^.?] Indicates that the character matches the task, except for. And?
* match [^!?] 0 or more times of the result
\ b match the boundary of the word
Apple matches apple (because it is case-sensitive, we add an I flag to the end of the regular expression)
\ b match the boundary of the word
[^.?] Indicates that the character matches the task, except for. And?
* match [^!?] 0 or more times of the result
. Matches any character except line breaks
? Match. 0 or 1 times of the matching result
G tells the regular expression engine to match all matches instead of stopping after the first match
I make search case-insensitive
Remove invalid characters from the file name
When you download a file, its name should not contain certain characters. For example, in Windows, the following characters are not valid in the file name and should be deleted:
:
"
/
\
| |
?
*
Using regular expressions, it is easy to remove invalid characters. Let's look at an example.
Const str = "https://en.wikipedia.org/" str.replace (/ [|:" *?\ /] + / g,'') / / = > "httpsen.wikipedia.org"
[] is called a character class, and JS matches one of the characters between the string and square brackets. In conjunction with the global (g) flag, we can effectively remove the characters in square brackets from the string.
Note that in the character class, the backslash has a special meaning and must be escaped with another backslash:\. The + operator represents a repeating character class to replace a series of invalid characters at the same time, which helps improve performance. Of course, it can be omitted, and it has no effect on the result.
Keep in mind that the second parameter of the replace () method must be an empty string unless you want to replace the invalid character with another character.
Several reserved names are also used internally by Windows to perform various tasks, and these reserved names are not allowed to be used as file names, as follows:
CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9
If you want to know more, Microsoft's Windows Development Center provides a detailed article on valid filenames.
To exclude reserved names, you can use the following code:
Str.replace (/ ^ (CON | PRN | AUX | NUL | COM1 | COM2 | COM3 | COM4 | COM5 | COM6 | COM7 | COM8 | COM9 | LPT1 | LPT2 | LPT3 | LPT4 | LPT5 | LPT6 | LPT7 | LPT8 | LPT9) $/ I, 'file')
The above code is mainly to replace the reserved word with the specified character.
Note that if the string contains other characters that are not reserved words, it will not be replaced. For example, "con" will be replaced, but "concord" will not be replaced, so this is a valid file name.
Where ^ matches the beginning of the string. It ensures that no other characters appear before the string we want to match, and that $matches the end of the string.
We can also simplify the rule in a simpler way by using character classes:
Str.replace (/ ^ (CON | PRN | AUX | NUL | COM [1-9] | LPT [1-9]) $/ I, 'file')
[1mur9] matches a number between 1 and 9.
Replace multiple spaces with a single space
When a web page is rendered, duplicate space characters are displayed as a single space. However, sometimes we want multiple spaces in user input or other data, and we just want to use a single space to represent it. You can easily do this with regular expressions:
Const str = "My opinions may have changed, but not the fact that I'm right." Str.replace (/\ s\ s but not the fact that I'm right. G,'') / / = > "My opinions may have changed, but not the fact that I'm right."
This regular expression contains only two metacharacters, an operator, and a flag bit:
\ s matches a single space character, including ASCII spaces, tabs, line feeds, carriage returns, vertical tabs, and page feeds
\ s matches a space character again
+ match the previous item one or more times, that is, match one or more spaces
G tells the regular expression engine to match all matches instead of stopping after the first match
The above result is to replace all white space characters that are repeated at least twice. Note that the result in the above example still starts with a white space character and should be deleted. To do this, simply add the trim () function to the end of the statement:
Str.replace (/\ s\ s). Trim () / / = > "My opinions may have changed, but not the fact that I'm right."
Remember that this code replaces any type of space characters with space (U + 0020) characters, including ASCII spaces, tabs, line feeds, carriage returns, vertical tabs, and page feeds. Therefore, if carriage returns immediately follow tabs, they will be replaced by spaces. If this is not our intention and only wants to replace the same type of space, use the following code instead:
Str.replace (/ (\ s)\ 1printer _ g,'$1') .trim ()
\ 1 is a backreference that matches the same character matched in the first pair of parentheses (\ s). To replace them, we use $1 in the second parameter of replace (), which inserts matching characters in parentheses.
Restrict users to enter only numbers or letters
A common form operation in Web development is to restrict user input. For example, we want to limit users to numbers or letters. Similarly, using rules, it is simple to use a character class to define the range of characters allowed, and then append a quantifier to specify the number of characters that can be repeated:
Const input1 = "John543"; const input2 = ": -)"; / ^ [A-Z0-9] + $/ i.test (input1); / / → true / ^ [A-Z0-9] + $/ i.test (input2); / / → false
The operation is as follows:
^ matches the beginning of the string, which ensures that no other characters appear before the string we want to match.
[A-Z0-9] matches characters between An and Z or between 0 and 9. Because this is case-sensitive, we use the I flag to indicate that case is ignored. Or, we can use [A-Za-z0-9] instead.
+ match one or more times. Therefore, the input must contain at least one non-blank alphanumeric character; otherwise, the match fails. If you want to make this field optional, you can use the * quantifier, which matches the previous item zero or more times.
$matches the end of the string.
Turn a URL into a link
Suppose we have one or more URLs in the text that are not HTML anchor elements, so we cannot click on them. We want to automatically convert URL to links. To do this, we first need to find the URL, and then wrap each URL in … Tag, and the href attribute used points to the URL:
Const str = "Visit https://en.wikipedia.org/ for more info."; str.replace (/\ b (https? | ftp | file):\ /\ /\ S+ [\ /\ w] / g,'$&') / / = > "Visit https://en.wikipedia.org/ for more info."
Take a look at how this code works:
\ b match the position of the word boundary
(https? | ftp | file) matches the characters https,http,ftp or file.
Literally match colons
\ / literally match forward slash characters
\ s matches any single character that is not a space
+ match the previous item one or more times
[\ /\ w] matches a forward slash or word character. Without this, the regular expression will match all punctuation marks at the end of the URL
G tells the regular expression engine to match all matches instead of stopping after the first match
$& in the second parameter of replace (), insert the matching substring into the replacement string
Delete duplicate words
Sometimes, we will find that the words of some articles are repeated, and it will be very troublesome if they are repeated all the time. Fortunately, regular can solve this problem with only one line of code:
Const str = "This this sentence has has double words." Str.replace (/\ b (\ w+)\ s +\ 1\ b/gi,'$1') / / = > "This sentence has double words."
\ b match the boundary of the word
\ w match word characters
+ match one or more of the previous item
\ 1 is a backreference that represents the text that matches in the first pair of parentheses
\ b match word boundaries
G tells the regular expression engine to match all matches instead of stopping after the first match
I ignore case
$1 represents the first text content of the group
Thank you for your reading, the above is the content of "what Java is convenient regular expression", after the study of this article, I believe you have a deeper understanding of the question of what Java is convenient regular expression, the specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.