Get the App
SLTechnology News&Howtos  ›  Development  › 

How does java use regular expressions to limit the number of special characters

Shulou Source: shulou.com Published: 2022-06-02 09:32:32 10月14日 Update

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!

Tags: Special character regular expression number limit symbol content more article good practical element only article look knowledge category answer result Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Huawei OPPO Reno MariaDB Shulou Information