In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how java uses regular expressions to limit the number of special characters. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Use regular expressions to limit the number of special characters package com.l.dubbo.service; import java.util.HashSet;import java.util.Set;import java.util.regex.Matcher;import java.util.regex.Pattern; public class ThreadPoolTest {public static boolean check1 (String context, String regEx) {Pattern pattern = Pattern.compile (regEx); Matcher matcher = pattern.matcher (context); String ss = "; String S2 =" Boolean flag = true; int count = 0; / / subsegments that conform to the regular expression are special symbols / / compare whether the subsegments are repeated or not. If they are repeated, there is more than one kind of special symbol while (matcher.find ()) {if (count = = 0) {ss = matcher.group (); / / System.out.println (ss). } else {S2 = matcher.group (); / / System.out.println (S2);} if (! (ss) .equals (S2) & & count > 0) {flag = false;} count++;} / / System.out.println (count); return flag } public static boolean check2 (String context, String regEx) {Pattern pattern = Pattern.compile (regEx); Matcher matcher = pattern.matcher (context); boolean flag = false; / / elements using set cannot be repeated to check whether there is only one special symbol Set set = new HashSet (); while (matcher.find ()) {set.add (matcher.group ()) } if (set.size () = = 1) {flag = true;} return flag;} public static void main (String [] args) {/ / S1 contains only'!' A special character String S1 = "dasdflowers a22 special characters!"; / / S2 contains'!' And'@ 'two special characters String S2 = "dasdrooma22 special characters!"; String regEx = "(? = [\\ x21 -\\ x7e] +) [^ A-Za-z0-9])"; System.out.println (check1 (S1, regEx)); System.out.println (check1 (S2, regEx)); System.out.println (check2 (S1, regEx)) System.out.println (check2 (S2, regEx));}}
Output result
True
False
True
False
Process finished with exit code 0
Java judges the regularity of qualified characters
Determine whether the answer is regular of ABCD.
Public class Test {public static void main (String [] args) {Pattern pattern = Pattern.compile ("^ [Amurd] + $"); System.out.println (pattern.matcher ("A"). Find (); System.out.println (pattern.matcher ("E"). Find ();} Thank you for reading! This is the end of this article on "how to use regular expressions to limit the number of special characters in java". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.