Java 正規表現の単一文字マッチングと事前定義文字の詳細な紹介
本篇文章给大家带来了关于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 中国語 Web サイトの他の関連記事を参照してください。

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック









Java の Weka へのガイド。ここでは、weka java の概要、使い方、プラットフォームの種類、利点について例を交えて説明します。

この記事では、Java Spring の面接で最もよく聞かれる質問とその詳細な回答をまとめました。面接を突破できるように。

Java 8は、Stream APIを導入し、データ収集を処理する強力で表現力のある方法を提供します。ただし、ストリームを使用する際の一般的な質問は次のとおりです。 従来のループにより、早期の中断やリターンが可能になりますが、StreamのForeachメソッドはこの方法を直接サポートしていません。この記事では、理由を説明し、ストリーム処理システムに早期終了を実装するための代替方法を調査します。 さらに読み取り:JavaストリームAPIの改善 ストリームを理解してください Foreachメソッドは、ストリーム内の各要素で1つの操作を実行する端末操作です。その設計意図はです

Java での日付までのタイムスタンプに関するガイド。ここでは、Java でタイムスタンプを日付に変換する方法とその概要について、例とともに説明します。

カプセルは3次元の幾何学的図形で、両端にシリンダーと半球で構成されています。カプセルの体積は、シリンダーの体積と両端に半球の体積を追加することで計算できます。このチュートリアルでは、さまざまな方法を使用して、Javaの特定のカプセルの体積を計算する方法について説明します。 カプセルボリュームフォーミュラ カプセルボリュームの式は次のとおりです。 カプセル体積=円筒形の体積2つの半球体積 で、 R:半球の半径。 H:シリンダーの高さ(半球を除く)。 例1 入力 RADIUS = 5ユニット 高さ= 10単位 出力 ボリューム= 1570.8立方ユニット 説明する 式を使用してボリュームを計算します。 ボリューム=π×R2×H(4

Java は、初心者と経験豊富な開発者の両方が学習できる人気のあるプログラミング言語です。このチュートリアルは基本的な概念から始まり、高度なトピックに進みます。 Java Development Kit をインストールしたら、簡単な「Hello, World!」プログラムを作成してプログラミングを練習できます。コードを理解したら、コマンド プロンプトを使用してプログラムをコンパイルして実行すると、コンソールに「Hello, World!」と出力されます。 Java の学習はプログラミングの旅の始まりであり、習熟が深まるにつれて、より複雑なアプリケーションを作成できるようになります。
