java @literal_Java中的Pattern LITERAL字段的示例

启用模式的文字分析。在此,所有字符(包括转义序列和元字符)都没有任何特殊含义,它们被视为文字字符。

例如,通常,如果您在给定的输入文本中搜索正则表达式“ ^ This”,则它将匹配以单词“ This”开头的行。

示例import java.util.regex.Matcher;

import java.util.regex.Pattern;

public class LTERAL_Example {

public static void main(String[] args) {

String input = "This is the first line\n"

+ "This is the second line\n"

+ "^This is the third line";

//正则表达式以MM-DD-YYY格式接受日期

String regex = "^This";

//创建一个Pattern对象

Pattern pattern = Pattern.compile(regex,Pattern.LITERAL);

//创建一个Matcher对象

Matcher matcher = pattern.matcher(input);

int count = 0;

while(matcher.find()) {

count++;

System.out.println(matcher.group());

}

System.out.println("Number of matches: "+count);

}

}

输出结果^This

Number of matches: 1

在文字模式下,元字符“ ^”没有意义,并且正则表达式“ ^ This”与确切的单词匹配。

示例import java.util.regex.Matcher;

import java.util.regex.Pattern;

public class LTERAL_Example {

public static void main(String[] args) {

String input = "This is the first line\n"

+ "This is the second line\n"

+ "^This is the third line";

//正则表达式以MM-DD-YYY格式接受日期

String regex = "^This";

//创建一个Pattern对象

Pattern pattern = Pattern.compile(regex,Pattern.LITERAL);

System.out.println("Usually it is printed as: \n"+input);

//创建一个Matcher对象

Matcher matcher = pattern.matcher(input);

int count = 0;

while(matcher.find()) {

count++;

System.out.println(matcher.group());

}

System.out.println("Number of matches: "+count);

}

}

输出结果Usually it is printed as:

This is the first line

This is the second line

^This is the third line

^This

Number of matches: 1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值