Java正则表达式程序,用于匹配括号'('或者')'
以下正则表达式接受带有括号的字符串 −
"^.*[\(\)].*$";
^ matches the starting of the sentence.
.* Matches zero or more (any) characters.
[()] matching parenthesis.
$ indicates the end of the sentence.
Example 1
Live Demo
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class SampleTest { public static void main( String args[] ) { String regex = "^.*[\(\)].*$"; //Reading input from user Scanner sc = new Scanner(System.in); System.out.println("Enter data: "); String input = sc.nextLine(); //Instantiating the Pattern class Pattern pattern = Pattern.compile(regex); //Instantiating the Matcher class Matcher matcher = pattern.matcher(input); //verifying whether a match occurred if(matcher.find()) { System.out.println("Input accepted"); }else { System.out.println("Not accepted"); } } }
Output 1
Enter data: sample(text) with parenthesis Input accepted
Output 2
Enter data: sample text Not accepted
Example 2
演示
import java.util.Scanner; public class Example { public static void main(String args[]) { //Reading String from user System.out.println("Enter email address: "); Scanner sc = new Scanner(System.in); String e_mail = sc.nextLine(); //Regular expression String regex = "^.*[\(\)].*$"; boolean result = e_mail.matches(regex); if(result) { System.out.println("Valid match"); } else { System.out.println("Invalid match"); } } }
Output 1
Enter email address: sample(text) with parenthesis Valid match
Output 2
Enter email address: sample text Invalid match
以上是Java正则表达式程序,用于匹配括号'('或者')'的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

本文分析了2025年的前四个JavaScript框架(React,Angular,Vue,Susve),比较了它们的性能,可伸缩性和未来前景。 尽管由于强大的社区和生态系统,所有这些都保持占主导地位,但它们的相对人口

本文介绍了SnakeyAml中的CVE-2022-1471漏洞,这是一个允许远程代码执行的关键缺陷。 它详细介绍了如何升级春季启动应用程序到Snakeyaml 1.33或更高版本的降低风险,强调了依赖性更新

Java的类上载涉及使用带有引导,扩展程序和应用程序类负载器的分层系统加载,链接和初始化类。父代授权模型确保首先加载核心类别,从而影响自定义类LOA

本文讨论了使用咖啡因和Guava缓存在Java中实施多层缓存以提高应用程序性能。它涵盖设置,集成和绩效优势,以及配置和驱逐政策管理最佳PRA

Node.js 20通过V8发动机改进可显着提高性能,特别是更快的垃圾收集和I/O。 新功能包括更好的WebSembly支持和精制的调试工具,提高开发人员的生产率和应用速度。

本文探讨了在黄瓜步骤之间共享数据的方法,比较方案上下文,全局变量,参数传递和数据结构。 它强调可维护性的最佳实践,包括简洁的上下文使用,描述性

本文使用lambda表达式,流API,方法参考和可选探索将功能编程集成到Java中。 它突出显示了通过简洁性和不变性改善代码可读性和可维护性等好处
