java 正则表达式
1、首先是说明一些容易混淆的符号
\w Matches any word character.
\W Matches any non-word character.
如果是在java中的话,需要双引号 \\w 才可以;而且这里需要注意大小写区分
\s 是代表空格
The *
character is a quantifier that means "zero or more times".
There is also a +
quantifier meaning "one or more times",
a ?
quantifier meaning "zero or one time",
2、说明中文处理的问题
Chinese and Japanese don't use the regular space character ' '. The languages use their own that is the same width as the characters. This is the character here ' ', you should write a manual trim function to check for that character at the beginning and end of the string.
You may be able to directly use the character if you convert your code file to unicode (if java will allow). Otherwise you will need to find the unicode character code for ' ' and check if the character code is at the beginning or end of the string.
The following link tells us that the ideographic space is 0xe38080 in UTF-8 and 0x3000 in UTF-16, and that Java's Character.isSpaceChar() function will return true. I would have thought String.trim() would have used this property to determine whether or not to trim though.
然后比如给定一个汉字,返回编码gurva
》》》》在Java中,String的getBytes()方法是得到一个操作系统默认的编码格式的字节数组。这个表示在不通OS下,返回的东西不一样!(所以不建议使用)
》》》》String.getBytes(String decode)方法会根据指定的decode编码返回某字符串在该编码下的byte数组表示,注意这个编码decode写的时候注意Charsets.UTF_8
如
byte[] b_gbk = "中".getBytes("GBK");
byte[] b_utf8 = "中".getBytes("UTF-8");
byte[] b_iso88591 = "中".getBytes("ISO8859-1");
将分别返回“中”这个汉字在GBK、UTF-8和ISO8859-1编码下的byte数组表示,此时b_gbk的长度为2,b_utf8的长度为3,b_iso88591的长度为1。
而与getBytes相对的,可以通过new String(byte[], decode)的方式来还原这个“中”字时,这个new
String(byte[], decode)实际是使用decode指定的编码来将byte[]解析成字符串。
String s_gbk = new String(b_gbk,"GBK");
String s_utf8 = new String(b_utf8,"UTF-8");
String s_iso88591 = new String(b_iso88591,"ISO8859-1");
3、去除中文空格
temp_class=CharMatcher.WHITESPACE.trimFrom(temp_class);
4、Matcher类的matches()和find( )方法区别
》》matches()
will only return true if the full string is matched. ---只能在全串中做matches()》》find()
will try to find the next occurrence within the substring that matches the regex. --可以在子串中做find() Note the emphasis on "the next". That means, the result of callingfind()
multiple times might not be the same. In addition, by usingfind()
you can callstart()
to return the position the substring was matche
final Matcher subMatcher = Pattern.compile("\\d+").matcher("skrf35kesruytfkwu4ty7sdfs"); System.out.println("Found: " + subMatcher.matches()); System.out.println("Found: " + subMatcher.find() + " - position " + subMatcher.start()); System.out.println("Found: " + subMatcher.find() + " - position " + subMatcher.start()); System.out.println("Found: " + subMatcher.find() + " - position " + subMatcher.start()); System.out.println("Found: " + subMatcher.find()); System.out.println("Found: " + subMatcher.find()); System.out.println("Matched: " + subMatcher.matches()); System.out.println("-----------"); final Matcher fullMatcher = Pattern.compile("^\\w+$").matcher("skrf35kesruytfkwu4ty7sdfs"); System.out.println("Found: " + fullMatcher.find() + " - position " + fullMatcher.start()); System.out.println("Found: " + fullMatcher.find()); System.out.println("Found: " + fullMatcher.find()); System.out.println("Matched: " + fullMatcher.matches()); System.out.println("Matched: " + fullMatcher.matches()); System.out.println("Matched: " + fullMatcher.matches()); System.out.println("Matched: " + fullMatcher.matches());
输出结果是:
Will output:
Found: false Found: true - position 4 Found: true - position 17 Found: true - position 20 Found: false Found: false Matched: false ----------- Found: true - position 0 Found: false Found: false Matched: true Matched: true Matched: true Matched: true说明:So, be careful when callingfind()
multiple times if theMatcher
object was not reset, even when the regex is surrounded with^
and$
to match the full string.
java 正则表达式的更多相关文章
- java正则表达式
java正则表达式 1.Java正则表达式的语法与示例: http://baike.xsoftlab.net/view/207.html 2.Java 正则表达式: http://www.runo ...
- Java正则表达式入门——转自RUNOOB.COM
Java 正则表达式 正则表达式定义了字符串的模式. 正则表达式可以用来搜索.编辑或处理文本. 正则表达式并不仅限于某一种语言,但是在每种语言中有细微的差别. Java正则表达式和Perl的是最为相似 ...
- Java 正则表达式详解
Java 提供了功能强大的正则表达式API,在java.util.regex 包下.本教程介绍如何使用正则表达式API. 正则表达式 一个正则表达式是一个用于文本搜索的文本模式.换句话说,在文本中搜索 ...
- 【转】详解Java正则表达式语法
(转自: http://www.jb51.net/article/76354.htm) 这篇文章主要介绍了Java正则表达式语法,包括常用正则表达式.匹配验证-验证Email是否正确以及字符串中查询字 ...
- java正则表达式【大全】
[正则表达式]文本框输入内容控制整数或者小数:^[0-9]+\.{0,1}[0-9]{0,2}$只能输入数字:"^[0-9]*$".只能输入n位的数字:"^\d{n}$& ...
- JAVA正则表达式:Pattern类与Matcher类详解(转)
java.util.regex是一个用正则表达式所订制的模式来对字符串进行匹配工作的类库包.它包括两个类:Pattern和Matcher Pattern 一个Pattern是一个正则表达式经编译后的表 ...
- JAVA正则表达式:Pattern类与Matcher类详解
java.util.regex是一个用正则表达式所订制的模式来对字符串进行匹配工作的类库包.它包括两个类:Pattern和Matcher Pattern 一个Pattern是一个正则表达式经编译后的表 ...
- Java 正则表达式[转载]
PS:转载自CSDN博客看上去很美 众所周知,在程序开发中,难免会遇到需要匹配.查找.替换.判断字符串的情况发生,而这些情况有时又比较复杂,如果用纯编码方式解决,往往会浪费程序员的时间及精力.因此,学 ...
- Java正则表达式的应用
在很多种情况下,我们都必须对字符串进行匹配,以便判断字符串的格式是否符合要求,对字符串中的内容进行提取.比如,我要从一段话aabdfe中,判断这段话是否有包含ab这个词,那么如果用if-else来判断 ...
- Java正则表达式实用教程
java.util.regex是一个用正则表达式所订制的模式来对字符串进行匹配工作的类库包.java.util.regex包主要包括以下三个类:Pattern.Matcher和PatternSynta ...
随机推荐
- JavaScript模板引擎原理,几行代码的事儿
一.前言 什么是模板引擎,说的简单点,就是一个字符串中有几个变量待定.比如: var tpl = 'Hei, my name is <%name%>, and I\'m <%age% ...
- sqilite学习
1,用代码插入数据 for (int i = 0; i < 100; i++) { NSString *nameStr = [NSString stringWithFormat:@ ...
- 修复 ThinkPHP3.2.3 抛出异常模块的一个BUG,关闭字段缓存功能
使用 ThinkPHP3.2.3 遇到一个奇怪的问题,正式环境上报错,提示 “页面错误!请稍后再试~” 为了查看到底出啥错误,哪里出错,于是在入口文件中加了一段代码,开启调试: defined('AP ...
- Daily Scrum Meeting ——ThirdDay
一.Daily Scrum Meeting照片 二.Burndown Chart 三.项目进展 1.完成了github上的文档整理 Transcend/ActivityHelper 2.主界面侧滑框的 ...
- C和指针 第十二章 使用结构和指针 双链表和语句提炼
双链表中每个节点包含指向当前和之后节点的指针,插入节点到双链表中需要考虑四种情况: 1.插入到链表头部 2.插入到链表尾部 3.插入到空链表中 4.插入到链表内部 #include <stdio ...
- Gyro
Gyro 2 Plugin gyro2.jsVersion 1.19HTML5 only 本插件使用手机或平板设备上的陀螺仪和加速度传感器来控制krpano中的浏览和观看方向.Gyro2插件对比旧的陀 ...
- myBatis学习笔记
java.lang.NullPointerException at cn.itcast.mybatis.dao.UserDaoImpl.findUserById(UserDaoImpl.java:22 ...
- PYTHON isinstance语法
def obj_len(arg): #isinstance(),判断是否是某一类 if isinstance(arg,str) or (isinstance(arg,list)) or (isinst ...
- yii2事务运用举例
直接上代码: $db = Yii::$app->db; $transaction = $db->beginTransaction(); //开启事务 try { // 更新member表 ...
- POJ推荐50题
此文来自北京邮电大学ACM-ICPC集训队 此50题在本博客均有代码,可以在左侧的搜索框中搜索题号查看代码. 以下是原文: POJ推荐50题1.标记“难”和“稍难”的题目可以看看,思考一下,不做要求, ...