请问java的正则表达式去空格 如何忽略空格去匹配

java用正则表达式判断空格的问题。_百度知道
java用正则表达式判断空格的问题。
我想获取两个标签之间的内容,但标签上又允许有多个空格,用正则表达式为什么不能这样写:String regex=&(?&=&p\\s*=&).*(?=&p&)&;
我想获取标签&p =&和&p&之间的内容,但是在第一个标签中,允许p和=之间有多个空格。请问这个正则表达式该如何写?
我有更好的答案
获取标签的内容?用documen.getElementByIdbytagname,innerHTML等方法更好。我完全不知道你写的正则表达式是什么意思。。
采纳率:45%
来自团队:
为您推荐:
其他类似问题
正则表达式的相关知识
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。Java正则表达式匹配多行
默认情况下 *中的 只能匹配出 以外的字符,如果遇到要匹配的字符串包含回车换行符(多行),则正则表达式遇到换行符后会停止,导致包含回车换行符的串不能正确匹配,解决的办法是:
1、使用Pattern和Matcher对
默认情况下.*中的.只能匹配出 以外的字符,如果遇到要匹配的字符串包含回车换行符(多行),则正则表达式遇到换行符后会停止,导致包含回车换行符的串不能正确匹配,解决的办法是:
1、使用Pattern和Matcher对象
设置Pattern模式为:Pattern.DOTALL
2、使用String.replaceAll()
正则表达式写法:
String reg = (?s)'.*';
下面是一个包含回车换行字符的正则表达式替换处理例子。
static String teststr = UAPPROJECT_ID='33decffd70002 ;
* 包含回车换行符的处理
public void testa(){
Pattern wp = Pattern.compile('.*?', Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
Matcher m = wp.matcher(teststr);
String result = m.replaceAll();
System.out.println(result: + result);
* 包含回车换行符的处理
public void testb(){
String result = teststr.replaceAll((?s)'.*?', );
System.out.println(result: + result);
正则表达式功能及应用
作者: 字体:[增加 减小] 类型:转载 自从jdk1.4推出java.util.regex包,就为我们提供了很好的Java正则表达式应用平台,因为Java正则表达式是一个很庞杂的体系。
正则表达式,就是用某种模式去匹配一类字符串的一个公式,正则表达式由一些普通字符和一些元字符(metacharacters)组成。普通字符包括大小写的字母和数字,而元字符则具有特殊的含义,不管是.Net平台还是Java平台,正则表达式表达的意思都是一样的,下面我们主要分析Java正则表达式中的功能和具体应用,希望文章对您有所帮助,仅供参考。
自从jdk1.4推出java.util.regex包,就为我们提供了很好的Java正则表达式应用平台,因为Java正则表达式是一个很庞杂的体系。
间隔 (' ')
换行 (' ')
回车 (' ')
d 数字 等价于[0-9]
D 非数字 等价于[^0-9]
s 空白符号 [
S 非空白符号 [^
w 单独字符 [a-zA-Z_0-9]
W 非单独字符 [^a-zA-Z_0-9]
 一个单词的边界
B 一个非单词的边界
G 前一个匹配的结束
^为限制开头
^java条件限制为以Java为开头字符
$为限制结尾
java$条件限制为以java为结尾字符
. 条件限制除 以外任意一个单独字符
java..条件限制为java后除换行外任意两个字符
加入特定限制条件「[]」
[a-z] 条件限制在小写a to z范围中一个字符
[A-Z] 条件限制在大写A to Z范围中一个字符
[a-zA-Z] 条件限制在小写a to z或大写A to Z范围中一个字符
[0-9] 条件限制在小写0 to 9范围中一个字符
[0-9a-z] 条件限制在小写0 to 9或a to z范围中一个字符
[0-9[a-z]] 条件限制在小写0 to 9或a to z范围中一个字符(交集)
[]中加入^后加再次限制条件「[^]」
[^a-z] 条件限制在非小写a to z范围中一个字符
[^A-Z] 条件限制在非大写A to Z范围中一个字符
[^a-zA-Z] 条件限制在非小写a to z或大写A to Z范围中一个字符
[^0-9] 条件限制在非小写0 to 9范围中一个字符
[^0-9a-z] 条件限制在非小写0 to 9或a to z范围中一个字符
[^0-9[a-z]] 条件限制在非小写0 to 9或a to z范围中一个字符(交集)
在限制条件为特定字符出现0次以上时,可以使用「*」
J* 0个以上J
.* 0个以上任意字符
J.*D J与D之间0个以上任意字符
在限制条件为特定字符出现1次以上时,可以使用「+」
J+ 1个以上J
.+ 1个以上任意字符
J.+D J与D之间1个以上任意字符
在限制条件为特定字符出现有0或1次以上时,可以使用「?」
JA? J或者JA出现
限制为连续出现指定次数字符「{a}」
文字a个以上,并且「{a,}」
J{3,} JJJ,JJJJ,JJJJJ,???(3次以上J并存)
文字个以上,b个以下「{a,b}」
J{3,5} JJJ或JJJJ或JJJJJ
两者取一「|」
Java|Hello Java或Hello
「()」中规定一个组合类型
比如,我查询中间的数据,可写作(.+?)
在使用Pattern.compile函数时,可以加入控制Java正则表达式的匹配行为的参数:
Pattern Pattern.compile(String regex, int flag)
flag的取值范围如下:
Pattern.CANON_EQ 当且仅当两个字符的正规分解(canonical decomposition)都完全相同的情况下,才认定匹配。比如用了这个标志之后,表达式a?会匹配?。默认情况下,不考虑规 范相等性(canonical equivalence)。
Pattern.CASE_INSENSITIVE(?i)
默认情况下,大小写不明感的匹配只适用于US-ASCII字符集。这个标志能让表达式忽略大小写进行匹配。要想对Unicode字符进行大小不明感的匹 配,只要将UNICODE_CASE与这个标志合起来就行了。
Pattern.COMMENTS(?x)
在这种模式下,匹配时会忽略Java正则表达式里的空格字符(译者注:不是指表达式里的\s,而是指表达式里的空格,tab,回车之类)。注释从#开始,一直到这行结束。可以通过嵌入式的标志来启用Unix行模式。
Pattern.DOTALL(?s)
在这种模式下,表达式'.'可以匹配任意字符,包括表示一行的结束符。默认情况下,表达式'.'不匹配行的结束符。
Pattern.MULTILINE(?m)
在这种模式下,'^'和'$'分别匹配一行的开始和结束。此外,'^'仍然匹配字符串的开始,'$'也匹配字符串的结束。默认情况下,这两个表达式仅仅匹配字符串的开始和结束。
Pattern.UNICODE_CASE(?u)
在这个模式下,如果你还启用了CASE_INSENSITIVE标志,那么它会对Unicode字符进行大小写不明感的匹配。默认情况下,大小写不敏感的匹配只适用于US-ASCII字符集。
Pattern.UNIX_LINES(?d)
在这个模式下,只有' '才被认作一行的中止,并且与'.','^',以及'$'进行匹配。抛开空泛的概念,下面写出几个简单的Java正则用例:
◆比如,在字符串包含验证时
复制代码 代码如下:
//查找以Java开头,任意结尾的字符串
Pattern pattern = Pattern.compile(^Java.*);
Matcher matcher = pattern.matcher(Java不是人);
boolean b= matcher.matches(); //当条件满足时,将返回true,否则返回false
System.out.println(b);
以多条件分割字符串时
复制代码 代码如下:
Pattern pattern = Pattern.compile([, |]+);
String[] strs = pattern.split(Java Hello World Java,Hello,,World|Sun);
for (int i=0;i System.out.println(strs[i]);
文字替换(首次出现字符)
复制代码 代码如下:
Pattern pattern = Pattern.compile(Java正则表达式);
Matcher matcher = pattern.matcher(Java正则表达式 Hello World,正则表达式 Hello World);
//替换第一个符合正则的数据
System.out.println(matcher.replaceFirst(Java));
文字替换(全部)
复制代码 代码如下:
Pattern pattern = Pattern.compile(Java正则表达式);
Matcher matcher = pattern.matcher(Java正则表达式 Hello World,正则表达式 Hello World);
//替换第一个符合正则的数据
System.out.println(matcher.replaceAll(Java));
文字替换(置换字符)
复制代码 代码如下:
Pattern pattern = Pattern.compile(Java正则表达式);
Matcher matcher = pattern.matcher(Java正则表达式 Hello World,正则表达式 Hello World );
StringBuffer sbr = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(sbr, Java);
matcher.appendTail(sbr);
System.out.println(sbr.toString());
验证是否为邮箱地址
复制代码 代码如下:
String str=.
Pattern pattern = Pattern.compile([\w\.\-]+@([\w\-]+\.)+[\w\-]+,Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(str);
System.out.println(matcher.matches());
去除html标记
复制代码 代码如下:
Pattern pattern = Pattern.compile(&.+?&, Pattern.DOTALL);
Matcher matcher = pattern.matcher();
String string = matcher.replaceAll();
System.out.println(string);
查找html中对应条件字符串
复制代码 代码如下:
Pattern pattern = Pattern.compile(href=(.+?));
Matcher matcher = pattern.matcher();
if(matcher.find())
System.out.println(matcher.group(1));
◆截取http://地址
复制代码 代码如下:
Pattern pattern = Pattern.compile((http://|https://){1}[\w\.\-/:]+);
Matcher matcher = pattern.matcher(dsdsdsfdf);
StringBuffer buffer = new StringBuffer();
while(matcher.find()){
buffer.append(matcher.group());
buffer.append( );
System.out.println(buffer.toString());
◆替换指定{}中文字
复制代码 代码如下:
String str = Java目前的发展史是由{0}年-{1}年;
String[][] object={new String[]{\{0\},1995},new String[]{\{1\},2007}};
System.out.println(replace(str,object));
public static String replace(final String sourceString,Object[] object) {
String temp=sourceS
for(int i=0;i String[] result=(String[])object[i];
Pattern pattern = Pattern.compile(result[0]);
Matcher matcher = pattern.matcher(temp);
temp=matcher.replaceAll(result[1]);
◆以正则条件查询指定目录下文件
复制代码 代码如下:
//用于缓存文件列表
private ArrayList files = new ArrayList();
//用于承载文件路径
private String _
//用于承载未合并的正则公式
private String _
class MyFileFilter implements FileFilter {
* 匹配文件名称
public boolean accept(File file) {
Pattern pattern = Pattern.compile(_regexp);
Matcher match = pattern.matcher(file.getName());
return match.matches();
} catch (Exception e) {
* 解析输入流
* @param inputs
FilesAnalyze (String path,String regexp){
getFileName(path,regexp);
* 分析文件名并加入files
* @param input
private void getFileName(String path,String regexp) {
File directory = new File(_path);
File[] filesFile = directory.listFiles(new MyFileFilter());
if (filesFile == null)
for (int j = 0; j & filesFile. j++) {
files.add(filesFile[j]);
* 显示输出信息
* @param out
public void print (PrintStream out) {
Iterator elements = files.iterator();
while (elements.hasNext()) {
File file=(File) elements.next();
out.println(file.getPath());
public static void output(String path,String regexp) {
FilesAnalyze fileGroup1 = new FilesAnalyze(path,regexp);
fileGroup1.print(System.out);
public static void main (String[] args) {
output(C:\,[A-z|.]*);
Java正则表达式的功用还有很多,事实上只要是字符处理,就没有正则做不到的事情存在。
红黑联盟&版权所有
Copyright&& 2017
All rights reserved.word转换html时,会留下很多格式,有些格式并不是我们所需要的,然而这些格式比真正的文章内容还要多,严重影响页面的加载速度,因此就需要找个一个好的解决方案把这些多余的格式个去掉。网上有很多去除word冗余格式的js版的正则表达式,这里只提供java版的正则表达式。
public static String clearWordFormat(String content) {
//把&P&&/P&转换成&/div&&/div&保留样式
//content = content.replaceAll("(&P)([^&]*&.*?)(&\\/P&)", "&div$2&/div&");
//把&P&&/P&转换成&/div&&/div&并删除样式
content = content.replaceAll("(&P)([^&]*)(&.*?)(&\\/P&)", "&p$3&/p&");
//删除不需要的标签
content = content.replaceAll("&[/]?(font|FONT|span|SPAN|xml|XML|del|DEL|ins|INS|meta|META|[ovwxpOVWXP]:\\w+)[^&]*?&", "");
//删除不需要的属性
content = content.replaceAll("&([^&]*)(?:lang|LANG|class|CLASS|style|STYLE|size|SIZE|face|FACE|[ovwxpOVWXP]:\\w+)=(?:'[^']*'|\"\"[^\"\"]*\"\"|[^&]+)([^&]*)&", "&$1$2&");
//删除&STYLE TYPE="text/css"&&/STYLE&及之间的内容
int styleBegin = content.indexOf("&STYLE");
int styleEnd = content.indexOf("&/STYLE&") + 8;
String style = content.substring(styleBegin, styleEnd);
content = content.replace(style, "");
去除不需要的标签
&[/]?(font|FONT|span|SPAN|xml|XML|del|DEL|ins|INS|meta|META|[ovwxpOVWXP]:\\w+)[^&]*?&
match an open tag character &
and optionally match a close tag sequence &/
(because we also want to
remove the closing tags)
match any of the list of unwanted tags: font,span,xml,del,ins
a pattern is given to match any of the namespace tags, anything beginning
with o,v,w,x,p, followed by a : followed by another word
match any attributes as far as the closing tag character &
the replace string for this regex is "", which will completely remove the
instances of any matching tags.
note that we are not removing anything between the tags, just the tags
themselves
去除不需要的属性
&([^&]*)(?:lang|LANG|class|CLASS|style|STYLE|size|SIZE|face|FACE|[ovwxpOVWXP]:\\w+)=(?:'[^']*'|\"\"[^\"\"]*\"\"|[^&]+)([^&]*)&
match an open tag character &
capture any text before the unwanted attribute (This is $1 in the replace
expression)
match (but don't capture) any of the unwanted attributes: class, lang,
style, size, face, o:p, v:shape etc.
there should always be an = character after the attribute name
match the value of the attribute by identifying the delimiters. these can be
single quotes, or double quotes, or no quotes at all.
for single quotes, the pattern is: ' followed by anything but a ' followed
similarly for double quotes.
for a non-delimited attribute value, i specify the pattern as anything
except the closing tag character &
lastly, capture whatever comes after the unwanted attribute in ([^&]*)
the replacement string &$1$2& reconstructs the tag without the
unwanted attribute found in the middle.
note: this only removes one occurence of an unwanted attribute, this is why
i run the same regex twice.
For example, take the html fragment: &p
class="MSO Normal" style="Margin-TOP:3em"&
the regex will only remove
one of these attributes.
Running the regex twice will remove the second one.
can't think of any reasonable cases where it would need to be run more than
浏览: 38049 次
来自: 成都
楼主,这个去掉冗余格式,是不是还要用IO读写一遍呢?
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'java 正则表达式 怎么去除不了 逗号,和& - ITeye问答
我的需求是要@xx后面可以用冒号,逗号,小于号或者空格来截取字符串。比如:@xx&br&,就可以匹配@xx&这个字符串?下面是我的代码,只可以匹配冒号和空格,小于号和逗号不起作用,是怎么回事?
protected final static Pattern referer_pattern = Pattern.compile("@([^@^\\s^:]{1,})([\\s::,,&]{0,1})");
protected final static String input = "@xx
Mouse2 opens@xx:the image in @xx,the feh image viewer @xx&/br&";
public static void test(){
Matcher matcher = referer_pattern.matcher(input);
while(matcher.find()){
System.out.println("matcher "+matcher.start()+"-"+matcher.end()+"
"+matcher.group());
matcher.reset();
(@.*?)(:|,|&| )
已解决问题
未解决问题

我要回帖

更多关于 正则表达式匹配空格 的文章

 

随机推荐