在Java开发中,有时会遇到一些比较别扭的规则从字符串中提取子字符串,规则无疑是写正则表达式来表达了,那按照正则来提取子字符串就会用到java.util.regex包。

java.util.regex是一个用正则表达式所订制的模式来对字符串进行匹配工作的类库包。 它包括两个类:Pattern和Matcher 。

Pattern: 一个Pattern是一个正则表达式经编译后的表现模式。

Matcher: 一个Matcher对象是一个状态机器,它依据Pattern对象做为匹配模式对字符串展开匹配检查。

首先一个Pattern实例订制了一个所用语法与PERL的类似的正则表达式经编译后的模式,然后一个Matcher实例在这个给定的Pattern实例的模式控制下进行字符串的匹配工作。

 package com.founder.mrp.util;

 import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 与String相关的工具方法集
* @author Jimmy
*
*/
public class StringUtil { public static String getMatcher(String regex, String source) {
String result = "";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(source);
while (matcher.find()) {
result = matcher.group(1);
}
return result;
} public static void main(String[] args) {
String url = "http://172.12.1.123/test.txt";
String regex = "(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})";
// String regex = "(\\d{1,3}\\.){1,3}(\\d{1,3})";
System.out.println(getMatcher(regex,url));
} }

参考资料:

Pattern、Matcher: http://www.cnblogs.com/playing/archive/2011/03/15/1984943.html

正则:http://deerchao.net/tutorials/regex/common.htm

Java按正则提取字符串的更多相关文章

  1. python3正则提取字符串里的中文

    # -*- coding: utf-8 -*- import re #过滤掉除了中文以外的字符 str = "hello,world!!%[545]你好234世界..." str ...

  2. java 正则提取字符串中的电话号码

    public static void test2() { String str = "张三:13539558064,李四:15626829748,赵六:13718952204"; ...

  3. 使用正则提取字符串中URL等信息

    一.说明 背景:最近在做同步京东商品信息时遇到一个问题,同步后的商品详情无法在富文本中修改,强制修改会导致图片无法正常显示,研究发现详情中的图片是在css的作为背景图指定的. 解决:经过多次尝试,最后 ...

  4. C# 正则提取字符串(提取一个或多个)

    实例一:string result = ""; string str = "大家好! <User EntryTime='2010-10-7' Email='zhan ...

  5. 正则提取字符串IP地址,返回IP列表

    public class Main { public static void main(String args[]) { String str = "10.11.90.1 asedfa 1. ...

  6. 使用Java正则表达式提取字符串中的数字一例

    直接上代码: String reg = "\\D+(\\d+)$"; //提取字符串末尾的数字:封妖塔守卫71 == >> 71 String s = monster. ...

  7. [原]Java面试题-将字符串中数字提取出来排序后输出

    [Title][原]Java面试题-将字符串中数字提取出来排序后输出 [Date]2013-09-15 [Abstract]很简单的面试题,要求现场在纸上写出来. [Keywords]面试.Java. ...

  8. 编程提取字符串"Java is a programming language"中的各个单词,并打印输出。

    import java.lang.String; import java.util.StringTokenizer; public class StringGetWord{ /* 编程提取字符串&qu ...

  9. java正则表达式应用--验证字符串是否为数字(转载)

    首先说一下java正则表达式的重点概念: 第一.相关类:Pattern.Matcher 第二.典型的调用顺序是 Pattern p = Pattern.compile("a*b") ...

随机推荐

  1. VOA学习-South Sudan Must Allow Aid

    South Sudan Must Allow Aid The United States is gravelyconcerned by the serious escalation of the hu ...

  2. 友元(friend)--初学篇

    友元:友好的元子,,,,呵呵呵 一般一个类中有私有(private),公有(public),和保护(protected)三种类型成员,而只有public成员才可以在类外被随便访问,protected只 ...

  3. redhat 6.4 双网卡绑定

    linux系统配置 1.redhat 6.4 双网卡绑定 1)#ethtool eth* //在服务器网口接网线至笔记本,确定各网口的配置文件: 2)切换目录 #cd /etc/sysconfig/n ...

  4. (转)JSON基础入门

    原文地址:http://kb.cnblogs.com/page/43982/ JSON 基础简单地说,JSON 可以将 JavaScript 对象中表示的一组数据转换为字符串,然后就可以在函数之间轻松 ...

  5. Redis 集群常见问题

    Redis集群相关问题 1:远程连接问题 远程连接保护模式下,需要做一些配置.

  6. WPF:保存窗口当前状态截图方法

    在制作软件使用手册或者操作示范市,比较常用方式有截图和视频制作.如果软件内置当前状态的截图和操作视频的导出功能,则将极大简化这方面的工作.使用wpf编写的UI界面,截图的导出功能逻辑相对简单,通用的实 ...

  7. JQuery的几种页面加载完执行三种方式

      jquery加载页面的方法(页面加载完成就执行) 1. $(function(){ $("#a").click(function(){ //adding your code h ...

  8. ios tableview 上加 textfiled

    ios tableview 上加 textfiled 首先附上我项目中用曾经用到的几张图  并说明一下我的用法: 图1: 图2: 图3: 心在你我说一下  我当初的实现 方法 ,希望能给你们一些  启 ...

  9. js window.open 参数设置

    function OpenWin(type, obj){ window.open ("http://www.baidu.com" + type, "_blank" ...

  10. ubuntu tengine 安装

    参考文章:http://wangyan.org/blog/install-openssl-from-source.html http://www1.site90.com/Linux/405.html ...