Java 正则表达式元字符示例

位置:首页>文章>详情   分类: Java教程 > 编程技术   阅读(332)   2023-06-26 07:54:18

正则表达式 定义字符串的搜索模式。对于给定的字符串,此模式可能匹配一次或多次或根本不匹配。正则表达式的缩写是regex。正则表达式可用于搜索、编辑和操作文本。

1. 元字符的Java正则表达式列表

正则表达式支持一些具有明确预定义含义的元字符特殊字符。以下元字符使某些常见模式更易于使用,例如'd' 而不是 [0..9]

<正文>
元字符 描述
d 任意数字,[0-9]的简写
D 非数字,[^0-9]的简写
s 一个空白字符,[tnx0brf]的缩写
小号 非空白字符,[^s]的简称
w 单词字符,[a-zA-Z_0-9]的简写
W 一个非单词字符[^w]
S+ 几个非空白字符
b 匹配单词边界。单词字符是 [a-zA-Z0-9_] 并且 b 匹配它的边界。

1. Java正则表达式元字符示例

package com.howtodoinjava.regex;

import junit.framework.Assert;

import org.junit.Test;

public class MetaCharactersTest
{
	@Test
	public void testAnyDigit()
	{
		/**
		 * Matches exactly 3 digits
		 * */
		String sampleText = "123";
		String regex = "d{3}";
		Assert.assertEquals(true, sampleText.matches(regex));
	}

	@Test
	public void testNonDigit()
	{
		/**
		 * Matches exactly 3 non digits
		 * */
		String sampleText = "abc";
		String regex = "d{3}";
		Assert.assertNotSame(true, sampleText.matches(regex));
	}

	@Test
	public void testWhiteSpaceCharacters()
	{
		/**
		 * Matches n number of spaces
		 * */
		String sampleText = "     ";
		String regex = "s{0,}";
		Assert.assertEquals(true, sampleText.matches(regex));
	}

	@Test
	public void testNonWhiteSpaceCharacter1()
	{
		/**
		 * Matches no space allowed
		 * */
		String sampleText = "abc123";
		String regex = "S{0,}";
		Assert.assertEquals(true, sampleText.matches(regex));
	}

	@Test
	public void testNonWhiteSpaceCharacter2()
	{
		/**
		 * Matches no space allowed with '+' sign
		 * */
		String sampleText = "abc123";
		String regex = "S+";
		Assert.assertEquals(true, sampleText.matches(regex));
	}

	@Test
	public void testWordCharacter()
	{
		/**
		 * Matches one or more word characters
		 * */
		String sampleText = "abc123";
		String regex = "w{0,}";
		Assert.assertEquals(true, sampleText.matches(regex));
	}

	@Test
	public void testNonWordCharacter()
	{
		/**
		 * Matches one or more word characters
		 * */
		String sampleText1 = "abc123";
		String sampleText2 = "!@#$";
		String regex = "W{0,}";
		Assert.assertNotSame(true, sampleText1.matches(regex));
		Assert.assertEquals(true, sampleText2.matches(regex));
	}

	@Test
	public void testWordBoundary()
	{
		/**
		 * Matches the word "abc123" exactly same
		 * */
		String sampleText = "abc123";
		String regex = "babc123b";
		Assert.assertEquals(true, sampleText.matches(regex));
	}
}

在这个 Java 正则表达式示例 中,我们学习了在正则表达式中使用元字符来评估文本字符串。

快乐学习!!

地址:https://www.cundage.com/article/regular-expressions-meta-characters.html

相关阅读

Learn to 删除单词之间多余的空格 from a 细绳 in Java. Given 3 examples 用一个空格替换多个空格 using a regular expression, ...
我最近收到了 Packt 出版的 Anubhava Srivastava 赠送的 “Java 9 Regular Expressions” 一书。对于任何想学习什么是正则表达式并从头开始的人来说...
Java 中的 while 循环 不断执行语句块,直到特定条件的计算结果为 true。一旦条件变为 false,while 循环就会终止。 作为最佳实践,如果一开始不知道迭代次数,建议使用 wh...
在此 java 正则表达式教程中,我们将学习使用正则表达式验证信用卡号.我们将从 VISA、Mastercard、Amex 和 Diners 等多家供应商那里了解信用卡号码的号码格式和验证。 1...
在此使用正则表达式的 Java 日期验证中,我们将学习验证简单的日期格式,例如 mm/dd/yy、mm/dd/yyyy , dd/mm/yy 和 dd/mm/yyyy。在这里,我们想使用一个正则...
正则表达式 定义字符串的搜索模式。对于给定的字符串,此模式可能匹配一次或多次或根本不匹配。正则表达式的缩写是regex。正则表达式可用于搜索、编辑和操作文本。 1. 元字符的Java正则表达式列...
在此 Java 正则表达式教程 中,我们将学习使用正则表达式来验证加拿大邮政编码。您也可以修改正则表达式以使其适合任何其他格式。 1. 什么是有效的加拿大邮政编码? 加拿大邮政编码是一个六字符的...
在此 java 正则表达式教程 中,我们将学习使用正则表达式来验证特定于英国。您也可以修改正则表达式以使其适合任何其他格式。 1. 什么是有效的英国邮政编码 英国的邮政编码(或邮政编码)由五到七...
通过示例了解 Java 14 中添加的 yield 关键字 以支持 switch 表达式。 1.yield关键字 yield是在Java 14中加入的,在switch表达式中使用。 Boolea...
我们可能会通过各种方式将不需要的非 ascii 字符放入文件内容或字符串中,例如从 MS Word 文档或 Web 浏览器复制和粘贴文本、PDF 到文本的转换或 HTML 到文本的转换。我们可能...