本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于单字符匹配和预定义字符的相关内容,下面一起来看一下,希望对大家有帮助。
推荐学习:《java视频教程》
一、需求
现有一个字符串,需满足如下要求:
[6, 18] 个字符
只能包含字母、数字、下划线
需以字母开头
如果字符串满足上述要求,返回 true,否则返回 false
public static boolean validString(String s) { return s.matches("[a-zA-Z][a-zA-Z0-9_]{5,17}"); }
正则表达式用极简的规则取代了复杂的验证逻辑
Regex Expression
正则表达式是一种通用的技术,适用于多种编程语言
二、单字符匹配(6个)
<span style="font-family:Microsoft Yahei, Hiragino Sans GB, Helvetica, Helvetica Neue, 微软雅黑, Tahoma, Arial, sans-serif">1. </span>[abc]
:字符串的某个位置(某一个字符)满足 a、b、c 中的一个
某个位置
:该【单字符匹配】放的位置
public class TestDemo { public static void main(String[] args) { String regex = "[zgq]"; System.out.println("z".matches(regex)); // true System.out.println("g".matches(regex)); // true System.out.println("q".matches(regex)); // true System.out.println("zgq".matches(regex)); // false }}
public class TestDemo { public static void main(String[] args) { String regex = "26[abc]3q"; System.out.println("26a3q".matches(regex)); // true System.out.println("26abc".matches(regex)); // false System.out.println("26b3q".matches(regex)); // true }}
2. [^abc]
:除了 a、b、c 之外的任意单个字符
public class TestDemo { public static void main(String[] args) { String regex = "[^cat]666"; System.out.println("c666".matches(regex)); // false System.out.println("a666".matches(regex)); // false System.out.println("t666".matches(regex)); // false System.out.println("bb666".matches(regex)); // false System.out.println("b666".matches(regex)); // true }}
public class TestDemo { public static void main(String[] args) { String regex1 = "[12345]666"; String regex2 = "[^1-5]666"; System.out.println("1666".matches(regex1)); // true System.out.println("3666".matches(regex1)); // true System.out.println("5666".matches(regex1)); // true System.out.println("6666".matches(regex1)); // false System.out.println("1666".matches(regex2)); // false System.out.println("3666".matches(regex2)); // false System.out.println("5666".matches(regex2)); // false System.out.println("6666".matches(regex2)); // true }}
3. [a-zA-z]
:匹配单个英文字母
public class TestDemo { public static void main(String[] args) { String regex = "[a-zA-Z]666"; System.out.println("6666".matches(regex)); // false System.out.println("b666".matches(regex)); // true }}
4. [a-d[1-6]]
:和 [a-d1-6]
一样(并集)
public class TestDemo { public static void main(String[] args) { String regex1 = "[a-d[1-6]]"; String regex2 = "[a-d1-6]"; System.out.println("a".matches(regex1)); // true System.out.println("e".matches(regex1)); // false System.out.println("1".matches(regex1)); // true System.out.println("7".matches(regex1)); // false System.out.println("a".matches(regex2)); // true System.out.println("e".matches(regex2)); // false System.out.println("1".matches(regex2)); // true System.out.println("7".matches(regex2)); // false }}
5. [zgq&&[god]]
:交集
public class TestDemo { public static void main(String[] args) { String regex1 = "[zgq&&[god]]"; System.out.println("q".matches(regex1)); // false System.out.println("d".matches(regex1)); // false System.out.println("g".matches(regex1)); // true }}
6. [zgq&&[god]]
:取差集
public class TestDemo { public static void main(String[] args) { String regex1 = "[zgq&&[^god]]"; System.out.println("q".matches(regex1)); // true System.out.println("d".matches(regex1)); // false System.out.println("g".matches(regex1)); // false System.out.println("z".matches(regex1)); // true // 取差集, 从字母 a 到字母 z 中去除字母 b 和 d String regex2 = "[a-z&&[^bd]]"; System.out.println("d".matches(regex2)); // false System.out.println("a".matches(regex2)); // true }}
三、预定义字符(7个)
预定义字符匹配的仍然是单个字符
【.】:任意单个字符
【\d】:数字
【\D】:非数字
【\s】:空白
【\S】:非空白
【\w】:字母(英文字母、下划线、数字)
【\W】:非英文字母
Java 中需以两个【\】开头表示预定义字符
public class TestDemo { public static void main(String[] args) { String r1 = "."; System.out.println("@".matches(r1)); // true System.out.println("庆".matches(r1)); // true System.out.println("I".matches(r1)); // true System.out.println(" ".matches(r1)); // true System.out.println(".".matches(r1)); // true }}
public class TestDemo { public static void main(String[] args) { // 匹配 java 文件 String r1 = ".\\.java"; System.out.println("a.java".matches(r1)); // true System.out.println("xjava".matches(r1)); // false System.out.println("5java".matches(r1)); // false }}
public class TestDemo { public static void main(String[] args) { String r1 = "[abc]"; String r2 = "\\[abc\\]"; System.out.println("a".matches(r1)); // true System.out.println("c".matches(r1)); // true System.out.println("[abc]".matches(r1)); // false System.out.println("a".matches(r2)); // false System.out.println("c".matches(r2)); // false System.out.println("[abc]".matches(r2)); // true }}
推荐学习:《java视频教程》
以上是详细介绍Java正则表达式之单字符匹配和预定义字符的详细内容。更多信息请关注PHP中文网其他相关文章!

javaispopularforcross-platformdesktopapplicationsduetoits“ writeonce,runanywhere”哲学。1)itusesbytbytybytecebytecodethatrunsonanyjvm-platform.2)librarieslikeslikeslikeswingingandjavafxhelpcreatenative-lookingenative-lookinguisis.3)

在Java中编写平台特定代码的原因包括访问特定操作系统功能、与特定硬件交互和优化性能。1)使用JNA或JNI访问Windows注册表;2)通过JNI与Linux特定硬件驱动程序交互;3)通过JNI使用Metal优化macOS上的游戏性能。尽管如此,编写平台特定代码会影响代码的可移植性、增加复杂性、可能带来性能开销和安全风险。

Java将通过云原生应用、多平台部署和跨语言互操作进一步提升平台独立性。1)云原生应用将使用GraalVM和Quarkus提升启动速度。2)Java将扩展到嵌入式设备、移动设备和量子计算机。3)通过GraalVM,Java将与Python、JavaScript等语言无缝集成,增强跨语言互操作性。

Java的强类型系统通过类型安全、统一的类型转换和多态性确保了平台独立性。1)类型安全在编译时进行类型检查,避免运行时错误;2)统一的类型转换规则在所有平台上一致;3)多态性和接口机制使代码在不同平台上行为一致。

JNI会破坏Java的平台独立性。1)JNI需要特定平台的本地库,2)本地代码需在目标平台编译和链接,3)不同版本的操作系统或JVM可能需要不同的本地库版本,4)本地代码可能引入安全漏洞或导致程序崩溃。

新兴技术对Java的平台独立性既有威胁也有增强。1)云计算和容器化技术如Docker增强了Java的平台独立性,但需要优化以适应不同云环境。2)WebAssembly通过GraalVM编译Java代码,扩展了其平台独立性,但需与其他语言竞争性能。

不同JVM实现都能提供平台独立性,但表现略有不同。1.OracleHotSpot和OpenJDKJVM在平台独立性上表现相似,但OpenJDK可能需额外配置。2.IBMJ9JVM在特定操作系统上表现优化。3.GraalVM支持多语言,需额外配置。4.AzulZingJVM需特定平台调整。

平台独立性通过在多种操作系统上运行同一套代码,降低开发成本和缩短开发时间。具体表现为:1.减少开发时间,只需维护一套代码;2.降低维护成本,统一测试流程;3.快速迭代和团队协作,简化部署过程。


热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

SublimeText3 Linux新版
SublimeText3 Linux最新版