java matcher函数,Java中的Matcher regionStart()方法与示例
java.util.regex.Matcher类表示执行各种匹配操作的引擎。该类没有构造函数,可以使用类java.util.regex.Pattern的matchs()方法创建/获取该类的对象。
这个(Matcher)类的regionStart()方法返回一个整数值,该整数值表示当前匹配器对象的起始索引。
例子1import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegionStartExample {
public static void main(String[] args) {
//Regular expression to accepts 6 to 10 characters
String regex = "[#]";
System.out.println("Enter a string: ");
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
//Creating a pattern object
Pattern pattern = Pattern.compile(regex);
//Creating a Matcher object
Matcher matcher = pattern.matcher(input);
//Setting region to the input string matcher.region(2, 4);
//Switching to transparent bounds
if(matcher.find()) {
System.out.println("Match found");
} else {
System.out.println("Match not found");
}
System.out.println("Starting of the region: "+ matcher.regionStart());
}
}
输出结果Enter a string:
#sample text
Match not found
Starting of the region: 2
例子2import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegionStartExample {
public static void main(String[] args) {
String regex = "(.*)(\\d+)(.*)";
String input = "124 This is a sample Text, 1234, with numbers in between.";
//Creating a pattern object
Pattern pattern = Pattern.compile(regex);
//Creating a Matcher object
Matcher matcher = pattern.matcher(input);
//Setting the region of the matcher
matcher.region(5, 20);
if(matcher.matches()) {
System.out.println("Match found");
} else {
System.out.println("Match not found");
}
System.out.print("Start of the region: "+matcher.regionStart());
}
}
输出结果Match not found
Start of the region: 5
java matcher函数,Java中的Matcher regionStart()方法与示例相关推荐
- Java 主函数 main 中的关键字 static
相信很多人在运行第一个可以运行的 Java 程序的时候都会要求写一个主函数. 然后很多人都会照葫芦画瓢的写一个下面的函数: public static void main(String[] args) ...
- Java 回调函数(传统方法和lambda方法)-一个例子秒懂
原文地址为: Java 回调函数(传统方法和lambda方法)-一个例子秒懂 背景: 想写一个工具类 Tool.java ,其中方法 public void readOneLine() 负责一行一行地 ...
- Java EasyExcel在Web网站中读写Excel的方法及示例代码
使用EasyExcel可以更容易简单在Web网站中读写Excel,本文主要介绍在Java Web网站中读写Excel方法及示例代码. 原文地址:Java EasyExcel在Web网站中读写Excel ...
- mysql去重函数的使用方法_MySQL中使用去重distinct方法的示例详解
一 distinct 含义:distinct用来查询不重复记录的条数,即distinct来返回不重复字段的条数(count(distinct id)),其原因是distinct只能返回他的目标字段,而 ...
- java final 函数_JAVA中Final的用法
1. 修饰基础数据成员的final 这是final的主要用途,其含义相当于C/C++的const,即该成员被修饰为常量,意味着不可修改.如java.lang.Math类中的PI和E是f ...
- java中3|4_关于java:函数参数中3个点的含义是什么?
本问题已经有最佳答案,请猛点这里访问. 我在读Android文档中的AsyncTask. private class DownloadFilesTask extends AsyncTask { pro ...
- java线程 yield_Java线程中yield与join方法的区别
长期以来,多线程问题颇为受到面试官的青睐.虽然我个人认为我们当中很少有人能真正获得机会开发复杂的多线程应用(在过去的七年中,我得到了一个机会),但是理解多线程对增加你的信心很有用.之前,我讨论了一个w ...
- java 类 函数,java常用类和函数
JAVA中常用类的常用方法_计算机软件及应用_IT/计算机_专业资料.. JAVA 中常用类的常用方法 一. java.lang.Object 类 1. clone ()方法 创建并返回此对象的... ...
- java如何重写_java中如何重写一个方法
方法的重写: 1.在子类中可以根据需要对从基类中继承来的方法进行重写. 2.重写的方法和被重写的方法必须具有相同方法名称.参数列表和返回类型. 3.重写方法不能使用比被重写的方法更严格的访问权限. 在 ...
最新文章
- 麻省理工深度学习基础公开课.ppt
- 淡谈泛型约束T(转)
- flask html 得到文本框 input的内容_【笔记7】HTML及其常见标签
- 在实际管理中学习管理
- netty系列之:从零到壹,搭建一个SOCKS代理服务器
- 计算机考研英语词汇书,求助:有知道电脑背考研英语单词的
- java非堆内存_java – 监视JVM的非堆内存使用情况
- 什么是Docker?看这一篇文章就够了
- linux centos7重启路由命令行,centos7 永久添加静态路由
- 信息学奥赛一本通 1112:最大值和最小值的差 | OpenJudge NOI 1.9 05
- C 创建基本图表 Chart Controls
- [C++] - 闭包(closure)
- ---M文件-函数式M文件/脚本式、m文件基本构成
- wince车机刷系统刷机包_刷机ROM大全,你刷过几个OS系统
- 【知识图谱系列】自适应深度和广度图神经网络模型
- 智慧医院软件(APP)功能列表
- smart原则_如何合理运用SMART原则高效达成工作目标?
- 金融量化分析【day112】:量化平台的使用-第一个策略
- go: cannot determine module path for source directory D:\go\code\SipSvrProxy\src\Common (outside GOP
- postgis转换简单geometry