[教程]安卓使用正则表达式

2014-05-03 02:19:39 -0400
package luoye.regex;
import java.util.regex.*;
import java.util.*;

public class DealString
{
		//分组
		public String fenZu(String str, String pattern, int group)
		{
				String temp=null;

				try
				{
						Pattern p= Pattern.compile(pattern);
						Matcher m = p.matcher(str);


						if (m.find())
						{
								temp = m.group(group);
						}

						return temp;
				}
				catch (Exception e)
				{
						return null;
				}
		}
		//是否找到字符串
		public boolean isFind(String str, String pattern)
		{
				int temp;

				temp=str.indexOf(pattern);
				if(temp==-1)
				{
						return false;
				}
				else
				{
						return true;
				}
		}
		//捕获
		public Object[] piPei(String str, String pattern)
		{
				ArrayList al=new ArrayList();
				try
				{
						Pattern p= Pattern.compile(pattern);
						Matcher m = p.matcher(str);

						while (m.find())
						{
								al.add(m.group());
						}

						return al.toArray();
				}
				catch (Exception e)
				{
						return null;
				}

		}
}
«Newer      Older»
Comment:
Name:

Back to home

Subscribe | Register | Login | N