百度
360搜索
搜狗搜索

java匹配正则表达式,JAVA里,如何匹配一个多位数?(正则表达式)详细介绍

本文目录一览: JAVA里,如何匹配一个多位数?(正则表达式)

String ss="33 33 33 30";
String[] num=ss.split(" ");
这样就可以弄成是num={"33","33","33","30"}的数组了。。
之后自己添加
进去,就不用我说了吧
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public final class Second {
public static void main( String[] args )
{
String regex = "[0-9]{1,}";
String str = " 33 444 5555";
Pattern pat = Pattern.compile(regex);
Matcher matcher = pat.matcher(str);
int i=0;
while (matcher.find()) {
String temp = str.substring(matcher.start()+7*i,matcher.end()+7*i);
str=str.replaceAll(temp, ""+temp+"");
i++;
}
System.out.println(str);
}
}
Stirng reg = "\d+";
//匹配的正则表达式 Regex r = new Regex(@"([1-9]\d*\.?\d*)|(0\.\d*[1-9])"); //开始匹配 Match m = r.Match(this.textBox1.Text); //得到具体数字 m.Groups[0].Value //接下来就是string的replace的活了。

java正则表达式匹配

进口的java.util.ArrayList;
进口的java.util.List;
进口java.util.regex.Matcher中;
进口的java 。util.regex.Pattern;
公共类Demo6 {
公共静态无效的主要(字串[] args){
名单的匹配=匹配(“serfwrf1231234dfsf2445”) ;
System.out.println(匹配);
}
公共静态;名单的匹配(字符串源){
名单的结果=新的ArrayList ();
字符串REG =;“\ \ D * [^ \ \ D *]”;。
匹配器M = Pattern.compile(REG)匹配器(源);
同时(m.find()){
弦乐R = m.group(0);
结果加入(R);
}
返回结果;
}
}
输入:aaa asdf sdf bbb asdfadf得到 : aaa-asdf--sdf---bbb 不知道是不是你想要的结果String ss = "aaa asdf sdf bbb asdfadf";ss = ss.substring(ss.indexOf("aaa",0), ss.indexOf("bbb",0)+"bbb".length());ss = ss.replaceAll("[\\s]", "-");System.out.println(ss);
是不是这样子,你要的效果?
String s="aaa bbb";
System.out.println(s.replace(" ", "-"));
你运行一下就知道了
直接s.replaceAll(" ", "-")不就达到你要的效果了么?
String ss = s.replaceAll("(?<=aaa)\\s(?=\\s*bbb)", "-")
这样行吗?

请问JAVA中正则表达式匹配怎么实现的!

java中正则表达式的匹配其实是利用不确定的有穷自动机(NFA)结合向上追溯的算法来实现的。
以下是示例代码:
import java.util.* ;
/*
* An NFAState is a node with a set of outgoing edges to other
* NFAStates.
*
* There are two kinds of edges:
*
* (1) Empty edges allow the NFA to transition to that state without
* consuming a character of input.
*
* (2) Character-labelled edges allow the NFA to transition to that
* state only by consuming the character on the label.
*
*/
class NFAState
{
/*
* WARNING:
*
* The maximum integer character code we'll match is 255, which
* is sufficient for the ASCII character set.
*
* If we were to use this on the Unicode character set, we'd get
* an array index out-of-bounds exception.
*
* A ``proper'' implementation of this would not use arrays but
* rather a dynamic data structure like Vector.
*/
public static final int MAX_CHAR = 255 ;
public boolean isFinal = false ;
private ArrayList

onChar[] = new ArrayList[MAX_CHAR] ;

private ArrayList

onEmpty = new ArrayList() ;

/*

* Add a transition edge from this state to next which consumes

* the character c.

*/

public void addCharEdge(char c, NFAState next) {

onChar[(int)c].add(next) ;

}

/*

* Add a transition edge from this state to next that does not

* consume a character.

*/

public void addEmptyEdge(NFAState next) {

onEmpty.add(next) ;

}

public NFAState () {

for (int i = 0; i < onChar.length; i++)

onChar[i] = new ArrayList() ;

}

public boolean matches(String s) {

return matches(s,new ArrayList()) ;

}

private boolean matches(String s, ArrayList visited) {

/*

* When matching, we work character by character.

*

* If we're out of characters in the string, we'll check to

* see if this state if final, or if we can get to a final

* state from here through empty edges.

*

* If we're not out of characters, we'll try to consume a

* character and then match what's left of the string.

*

* If that fails, we'll ask if empty-edge neighbors can match

* the entire string.

*

* If that fails, the match fails.

*

* Note: Because we could have a circular loop of empty

* transitions, we'll have to keep track of the states we

* visited through empty transitions so we don't end up

* looping forever.

*/

if (visited.contains(this))

/* We've found a path back to ourself through empty edges;

* stop or we'll go into an infinite loop. */

return false ;

阅读更多 >>>  免费的java资源网站,小弟跪求好的JAVA网站制作的菜鸟教程and书

/* In case we make an empty transition, we need to add this

* state to the visited list. */

visited.add(this) ;

if (s.length() == 0) {

/* The string is empty, so we match this string only if

* this state is a final state, or we can reach a final

* state without consuming any input. */

if (isFinal)

return true ;

/* Since this state is not final, we'll ask if any

* neighboring states that we can reach on empty edges can

* match the empty string. */

for (NFAState next : onEmpty) {

if (next.matches("",visited))

return true ;

}

return false ;

} else {

/* In this case, the string is not empty, so we'll pull

* the first character off and check to see if our

* neighbors for that character can match the remainder of

* the string. */

int c = (int)s.charAt(0) ;

for (NFAState next : onChar[c]) {

if (next.matches(s.substring(1)))

return true ;

}

/* It looks like we weren't able to match the string by

* consuming a character, so we'll ask our

* empty-transition neighbors if they can match the entire

* string. */

for (NFAState next : onEmpty) {

if (next.matches(s,visited))

return true ;

}

return false ;

}

}

}

/*

* Here, an NFA is represented by an entry state and an exit state.

*

* Any NFA can be represented by an NFA with a single exit state by

* creating a special exit state, and then adding empty transitions

* from all final states to the special one.

*

*/

public class NFA

{

public NFAState entry ;

public NFAState exit ;

public NFA(NFAState entry, NFAState exit) {

this.entry = entry ;

this.exit = exit;

}

public boolean matches(String str) {

return entry.matches(str);

}

/*

* c() : Creates an NFA which just matches the character `c'.

*/

public static final NFA c(char c) {

NFAState entry = new NFAState() ;

NFAState exit = new NFAState() ;

exit.isFinal = true ;

entry.addCharEdge(c,exit) ;

return new NFA(entry,exit) ;

}

/*

* e() : Creates an NFA which matches the empty string.

*/

public static final NFA e() {

NFAState entry = new NFAState() ;

NFAState exit = new NFAState() ;

entry.addEmptyEdge(exit) ;

exit.isFinal = true ;

return new NFA(entry,exit) ;

}

/*

* rep() : Creates an NFA which matches zero or more repetitions

* of the given NFA.

*/

public static final NFA rep(NFA nfa) {

nfa.exit.addEmptyEdge(nfa.entry) ;

nfa.entry.addEmptyEdge(nfa.exit) ;

return nfa ;

}

/*

* s() : Creates an NFA that matches a sequence of the two

* provided NFAs.

*/

public static final NFA s(NFA first, NFA second) {

first.exit.isFinal = false ;

second.exit.isFinal = true ;

first.exit.addEmptyEdge(second.entry) ;

return new NFA(first.entry,second.exit) ;

}

/*

* or() : Creates an NFA that matches either provided NFA.

*/

public static final NFA or(NFA choice1, NFA choice2) {

choice1.exit.isFinal = false ;

choice2.exit.isFinal = false ;

NFAState entry = new NFAState() ;

NFAState exit = new NFAState() ;

exit.isFinal = true ;

entry.addEmptyEdge(choice1.entry) ;

entry.addEmptyEdge(choice2.entry) ;

choice1.exit.addEmptyEdge(exit) ;

choice2.exit.addEmptyEdge(exit) ;

return new NFA(entry,exit) ;

}

/* Syntactic sugar. */

public static final NFA re(Object o) {

if (o instanceof NFA)

return (NFA)o ;

else if (o instanceof Character)

return c((Character)o) ;

else if (o instanceof String)

return fromString((String)o) ;

else {

throw new RuntimeException("bad regexp") ;

}

}

public static final NFA or(Object... rexps) {

NFA exp = rexps[0] ;

for (int i = 1; i < rexps.length; i++) {

exp = or(exp,re(rexps[i])) ;

}

return exp ;

}

public static final NFA s(Object... rexps) {

NFA exp = e() ;

for (int i = 0; i < rexps.length; i++) {

exp = s(exp,re(rexps[i])) ;

}

return exp ;

}

public static final NFA fromString(String str) {

if (str.length() == 0)

return e() ;

else

return s(re(str.charAt(0)),fromString(str.substring(1))) ;

}

public static void main(String[] args) {

NFA pat = s(rep(or("foo","bar")),"") ;

String[] strings =

{ "foo" , "bar" ,

"foobar", "farboo", "boofar" , "barfoo" ,

"foofoobarfooX" ,

"foofoobarfoo" ,

} ;

for (String s : strings) {

System.out.println(s + "\t:\t" +pat.matches(s)) ;

}

}

}

详细参考:http://matt.might.net/articles/implementation-of-nfas-and-regular-expressions-in-java/

正则表达式是匹配响应的字符串,但这个字符串可能比较复杂,用一般的表达可能不是很清晰,简单点说就是正则表达式表达的东西无二义性,

从你的:

String s=new String("0.0.0.0 0.255.255.255 CHINA 中国1");

来看,你只要用简单的:

String s1 = s.replaceFirst("255 ","255,");就可以解决问题啊!

为什么要用正则呢?

如果想用,可以参考我写的IPV4匹配规则:

IPV4地址匹配,可以检查每个段在0-255,可以检测非法IP

测试数据:

192.168.1.1,

135.0.0.0,

1.1.1.1

178.153.558,259

12.12.12.12

1023.2562.12.10,

14.15.213.2555

正则表达式:

((2[0-5][0-5]|1\d\d|\d\d|\d)\.){3}(2[0-5][0-5]|1\d\d|\d\d|\d)

匹配结果:

127.0.0.1,

192.168.1.1,

135.0.0.0,

1.1.1.1

12.12.12.12

String subjectString=new String("0.0.0.0 0.255.255.255 CHINA 中国1");

String resultString = null;

try {

Pattern regex = Pattern.compile("(\\d) ");

Matcher regexMatcher = regex.matcher(subjectString);

try {

resultString = regexMatcher.replaceAll("$1,");

} catch (IllegalArgumentException ex) {

// Syntax error in the replacement text (unescaped $ signs?)

} catch (IndexOutOfBoundsException ex) {

// Non-existent backreference used the replacement text

}

} catch (PatternSyntaxException ex) {

// Syntax error in the regular expression

}

Java中正则表达式匹配的语法规则:

以下是整理出来的Java下运用正则表达式实现匹配的程序案例,代码如下:

package org.luosijin.test;import java.util.regex.Matcher;import java.util.regex.Pattern;/** * 正则表达式 * @version V5.0 * @author Admin * @date 2015-7-25 */public class Regex { /** * @param args * @author Admin * @date 2015-7-25 */ public static void main(String[] args) { Pattern pattern = Pattern.compile("b*g"); Matcher matcher = pattern.matcher("bbg"); System.out.println(matcher.matches()); System.out.println(pattern.matches("b*g","bbg")); //验证邮政编码 System.out.println(pattern.matches("[0-9]{6}", "200038")); System.out.println(pattern.matches("//d{6}", "200038")); //验证电话号码 System.out.println(pattern.matches("[0-9]{3,4}//-?[0-9]+", "02178989799")); getDate("Nov 10,2009"); charReplace(); //验证身份证:判断一个字符串是不是身份证号码,即是否是15或18位数字。 System.out.println(pattern.matches("^//d{15}|//d{18}$", "123456789009876")); getString("D:/dir1/test.txt"); getChinese("welcome to china,江西奉新,welcome,你!"); validateEmail("luosijin123@163.com"); } /** * 日期提取:提取出月份来 * @param str * @author Admin * @date 2015-7-25 */ public static void getDate(String str){ String regEx="([a-zA-Z]+)|//s+[0-9]{1,2},//s*[0-9]{4}"; Pattern pattern = Pattern.compile(regEx); Matcher matcher = pattern.matcher(str); if(!matcher.find()){ System.out.println("日期格式错误!"); return; } System.out.println(matcher.group(1)); //分组的索引值是从1开始的,所以取第一个分组的方法是m.group(1)而不是m.group(0)。 } /** * 字符替换:本实例为将一个字符串中所有包含一个或多个连续的“a”的地方都替换成“A”。 * * @author Admin * @date 2015-7-25 */ public static void charReplace(){ String regex = "a+"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher("okaaaa LetmeAseeaaa aa booa"); String s = matcher.replaceAll("A"); System.out.println(s); } /** * 字符串提取 * @param str * @author Admin * @date 2015-7-25 */ public static void getString(String str){ String regex = ".+/(.+)$"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(str); if(!matcher.find()){ System.out.println("文件路径格式不正确!"); return; } System.out.println(matcher.group(1)); } /** * 中文提取 * @param str * @author Admin * @date 2015-7-25 */ public static void getChinese(String str){ String regex = "[//u4E00-//u9FFF]+";//[//u4E00-//u9FFF]为汉字 Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(str); StringBuffer sb = new StringBuffer(); while(matcher.find()){ sb.append(matcher.group()); } System.out.println(sb); } /** * 验证Email * @param email * @author Admin * @date 2015-7-25 */ public static void validateEmail(String email){ String regex = "[0-9a-zA-Z]+@[0-9a-zA-Z]+//.[0-9a-zA-Z]+"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(email); if(matcher.matches()){ System.out.println("这是合法的Email"); }else{ System.out.println("这是非法的Email"); } }}

阅读更多 >>>  java数组输入,如何在java控制台输入数组?

JAVA正则表达式匹配(br )

String str = "
";
String reg = "(?i)\\

";

System.out.println (str.matches (reg));

java正则表达式匹配

输出行号吗?String line = "";int lineNum = 0;while(读取一行){ lineNum++; //一行的字符串col String third = col.split("|")[2]; if(3<=Integer.parseInt(third)){ line = line + lineNum +" "; }}System.out.println("匹配的行:"+line);
\|\d+\|
ad
(?<=|)[3-9][0-9]*(?=|)
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test01 {
public static void main(String[] args){
String[] strs = new String[]{};//填入要匹配的东西
Pattern p = Pattern.compile("[^\\|]+\\|[^\\|]+\\|(\\d+)\\|");
for (String string : strs) {
Matcher m = p.matcher(string);
if (m.find()) {
String numStr = m.group(1);
int num = Integer.parseInt(numStr);
if (num > 2) {
System.out.println(string);
}
}
}
}
}

java正则表达式

  1、Java 正则表达式:

  正则表达式定义了字符串的模式。正则表达式可以用来搜索、编辑或处理文本。正则表达式并不仅限于某一种语言,但是在每种语言中有细微的差别。

  2、正则表达式实例

  一个字符串其实就是一个简单的正则表达式,例如 Hello World 正则表达式匹配 Hello World 字符串。.(点号)也是一个正则表达式,它匹配任何一个字符如:a 或 1。

  3、java.util.regex 包主要包括以下三个类:

  (1)Pattern 类:

  pattern 对象是一个正则表达式的编译表示。Pattern 类没有公共构造方法。要创建一个 Pattern 对象,你必须首先调用其公共静态编译方法,它返回一个 Pattern 对象。该方法接受一个正则表达式作为它的第一个参数。

  (2)Matcher 类:

  Matcher 对象是对输入字符串进行解释和匹配操作的引擎。与Pattern 类一样,Matcher 也没有公共构造方法。你需要调用 Pattern 对象的 matcher 方法来获得一个 Matcher 对象。

  (3)PatternSyntaxException:

  PatternSyntaxException 是一个非强制异常类,它表示一个正则表达式模式中的语法错误。

java正则表达式匹配

  三 括号 或匹配
  在正则表达式中 由于[]号只能做单个元素的匹配 这样会限制正则表达式的作用 如何做到多个元素的匹配呢?用()实现
  ()可以进行多个元素的匹配 例如:t(a|e|i|o|oo)n |在正则表达式中代表 或 的意思 即匹配的字符串只要满足()
  中任意一项元素的匹配 该正则表达式则返回true 见代码示例:
   public class RegExp {

   private Pattern patt;
   private Matcher matcher;
   /**
   * 括号 或匹配:想要匹配 toon 可以使用 | 操作符 | 操作符的基本意义就是 或 运算
   * 要匹配 toon 使用 t(a|e|i|o|oo)n 正则表达式
   * 不能使用方扩号 因为方括号只允许匹配单个字符;必须使用圆括号 ()
   * @param regStr
   * @param regex
   * @return
   */
   public boolean bracketReg(String regStr String regex){
   return monRegExp(regStr regex);
   }
   private boolean monRegExp(String regStr String regex){
   boolean wildcard_Res=false;
   patt=pile(regex);
   matcher=patt matcher(regStr);
   wildcard_Res= matcher find();
   return wildcard_Res;
   }
   }
   public class TestRegExp {
   public static void main(String[] args) {
   RegExp re=new RegExp();
   boolean wildcard_Res=false;
   //括号 或匹配
   wildcard_Res=re bracketReg( toon t(aoe|oo)n );
   System out println(wildcard_Res);
   //输出:wildcard_Res=true
   }
  注:在用()进行匹配时 凡是在()中有多个元素连续紧挨着出现时 必须这几个元素在匹配的字符串中也连续紧挨着出现 且不能出现多余的元素 否则匹配不会成功 见代码示例:
   public class RegExp {
   private Pattern patt;
   private Matcher matcher;
   /**
   * 括号 或匹配:想要匹配 toon 可以使用 | 操作符 | 操作符的基本意义就是 或 运算
   * 要匹配 toon 使用 t(a|e|i|o|oo)n 正则表达式
   * 不能使用方扩号 因为方括号只允许匹配单个字符;必须使用圆括号 ()
   * @param regStr
   * @param regex
   * @return */
   public boolean bracketReg(String regStr String regex){
   return monRegExp(regStr regex);
   }
   private boolean monRegExp(String regStr String regex){
   boolean wildcard_Res=false;
   patt=pile(regex);
   matcher=patt matcher(regStr);
   wildcard_Res= matcher find();
   return wildcard_Res;
   }
   }
  
   public class TestRegExp {
   public static void main(String[] args) {
   RegExp re=new RegExp();
   boolean wildcard_Res=false;
   //括号 或匹配
   wildcard_Res=re bracketReg( taoehn t(aoe|oo)n );
   System out println(wildcard_Res);
   //输出:wildcard_Res=false
lishixinzhi/Article/program/Java/hx/201311/25639

阅读更多 >>>  java反射面试,Java高级开发必须懂的反射是什么样的?

java 怎么用正则表达式拿出一篇文章中匹配

利用正则表达式从给定的字符串中取出符合匹配规则的字符串的Java程序如下:
import java.util.regex.Matcher;import java.util.regex.Pattern;public class E { public static void main(String[] args) { Pattern p = Pattern.compile("[A-Za-z]+");//设定匹配规则为取出字符串中的字母 Matcher m = p.matcher("12sifiwq820aufu");//与字符串匹配 while(m.find()){ System.out.println(m.group()); } }}运行结果:
sifiwqaufu

求教java匹配规定函数的正则表达式怎么写

/**
* 去掉字符串中匹配 的字符串
*
* @author
zhujie
* @return String regex 要替换的内容 value 字符串 state 替换的内容变成什么
*/
public static String toRegex(String regex, String value, String state) {
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(value);
StringBuffer sb = new StringBuffer();
while (m.find()) {
m.appendReplacement(sb, state);
}
m.appendTail(sb);
return
sb.toString();
}

网站数据信息

"java匹配正则表达式,JAVA里,如何匹配一个多位数?(正则表达式)"浏览人数已经达到16次,如你需要查询该站的相关权重信息,可以点击进入"Chinaz数据" 查询。更多网站价值评估因素如:java匹配正则表达式,JAVA里,如何匹配一个多位数?(正则表达式)的访问速度、搜索引擎收录以及索引量、用户体验等。 要评估一个站的价值,最主要还是需要根据您自身的需求,如网站IP、PV、跳出率等!