Java正则表达式获取中括号之间的内容
参考:
求一个正则表达式提取中括号里的内容 [问题点数:80分]CSDN论坛 > Java > Web 开发
正则表达式 - 菜鸟教程
不包含中括号
正则表达式如下:
\\[(.*?)]
注:
.匹配除换行符\n之外的任何单字符;
*匹配前面的子表达式零次或多次;
?匹配前面的子表达式零次或一次;
()标记一个子表达式的开始和结束位置;
\[匹配[字符。[是特殊字符需要转义;
\特殊字符,前面还需要转义字符\。
包含中括号
(\\[(.*?)])
外加小括号,表示[]也在匹配结果之内。
代码(不包含中括号)
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegularExpression {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String input = sc.nextLine();
String regex = "\\[(.*?)]";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
System.out.println(matcher.group(1));
}
}
sc.close();
}
}
样例
ab]cd[ef]gh[ij[kl]mn[op]qr]st[uv
ef
ij[kl
op
Java正则表达式获取中括号之间的内容的更多相关文章
- Java 正则表达式获取两个字符中间的内容
利用 正则表达式 获取两个字符串中间的值 直接上代码吧,不是很难. public static void main(String[] args) { // 内容 String value = &quo ...
- java 正则表达式获取匹配和非获取匹配
package test1; import java.util.regex.Matcher; import java.util.regex.Pattern; public class TestExp ...
- php用正则表达式获取网站的标题内容
已知网站的网址,用php获取网站的内容. 编写正则表达式. 用preg_match_all函数获取标题内容. $url='http://www.m-ivi.com'; $content=file_ge ...
- iPhone开发--正则表达式获取字符串中的内容
缘起: 想获取字符串中指定的字符,考虑用正则表达式,遂写了如下的代码: NSString *htmlStr = @"oauth_token=1a1de4ed4fca40599c5e5cfe0 ...
- java正则表达式获取指定HTML标签的指定属性值
package com.mmq.regex; import java.util.ArrayList; import java.util.List; import java.util.regex.Mat ...
- 正则表达式获取TABLE里的内容
//过滤\n 转换成空 String withoutNString=message.Replace("\n", ""); ...
- Java正则表达式获取网页所有网址和链接文字
; pos1= urlContent.indexOf(strAreaBegin)+strAreaBegin.length(); pos2=urlContent.inde ...
- java正则表达式取括号里面的内容
public static String changeCompName(String compName){ String NewCompName=""; //cm1230NHL6X ...
- java 正则表达式获取值
@Test public void testtest() { String test = "hahahhehe sendCode\":\"12367890123rsdfs ...
随机推荐
- mongo最大连接数查看
进入客户端 mongo 输入查看命令 db.serverStatus().connections
- Wrapper: Error - Unable to execute Java command
在64位的系统下 将短信程序运行于服务中,出现以下错误: Error: [size=14px; line-height: 26px;]FATAL | wrapper | 2012/06/18 17 ...
- 在java的Map集合中,怎样更改value的值
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/chenyao1994/article/de ...
- Spring AOP设计
Spring IOC设计到的设计模式: 工厂模式,模板方法模式,单例模式 Spring AOP涉及到的设计模式: 工厂模式,代理模式 1.Spring AOP目标 将分散在程序各处的横切关注点剥离出来 ...
- ActiveMQ处理模式
一.PTP处理模式(Queue) 消息生产者生产消息发送到queue中,然后消息消费者从queue中取出并且消费消息. 消息被消费以后,queue中不再有存储,所以消息消费者不可能消费到已经被消费的消 ...
- 约翰·麦斯威尔 | John C. Maxwell | A leader is one who knows the way, goes the way, and shows the way.
约翰·麦斯威尔_百度百科https://baike.baidu.com/item/%E7%BA%A6%E7%BF%B0%C2%B7%E9%BA%A6%E6%96%AF%E5%A8%81%E5%B0%9 ...
- SynchronizedStack -- tomcat8同步栈
同步栈(安全栈): org.apache.tomcat.util.collections.SynchronizedStack通过stack栈锁来控制栈中获取的类T.通过push.pop和clear方法 ...
- mobile crane 1
void MobileCrane::rotateRope2() { //double r_angle1 = rotateRope + rorate3; //std::cout << &qu ...
- Spring中好玩的注解和接口
测试中: 一.unit中集中基本注解,是必须掌握的. @BeforeClass – 表示在类中的任意public static void方法执行之前执行 @AfterClass – 表示在类中的任意p ...
- MySQL数据库双机热备------主-主备份配置
MySQL数据库双机热备------主-主备份配置 实验环境: 主1数据库 192.168.1.1 centos6.5 x86_64 +MySQL5.5.35 主2数据库192.168.1.2 Wi ...