http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 8575|回复: 2

[新手开发之旅] Android新手开发之旅-Android中如何使用正则

[复制链接]
发表于 2019-1-21 17:53:09 | 显示全部楼层 |阅读模式


Android新手开发之旅目录




用法比较简单,举个例子大家就明白了


activity_main.xml:
[XML] 纯文本查看 复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="20dp">

    <EditText
        android:id="@+id/et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入" />

    <Button
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="判断" />
</LinearLayout>



MainActivity:
[Java] 纯文本查看 复制代码
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final EditText et = (EditText) findViewById(R.id.et);
        Button btn = (Button) findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /**
                 *  130-139这十个前三位已经全部开通,后面8位每一位都是0-9之间的任意数;
                 *  14开头的目前只有145、147、149三位,后面8位每一位都是0-9之间的任意数;
                 *  15开头的除了154以外第三位可以随意取,后面8位每一位都是0-9之间的任意数;
                 *  180-189这十个前三位已经全部开通,后面8位每一位都是0-9之间的任意数;
                 *  17开头的目前有170、171、173、175、176、177、178这七位,后面8位每一位都是0-9之间的任意数;
                 */
                String regEx = "^((13[0-9])|(14[5,7,9])|(15[^4])|(18[0-9])|(17[0,1,3,5,6,7,8]))\\d{8}$";
                Pattern p = Pattern.compile(regEx);
                Matcher m = p.matcher(et.getText());
                boolean matches = m.matches();
                if (matches) {
                    Toast.makeText(MainActivity.this, "符合规则", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity.this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }
}




           








1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2019-1-22 08:38:47 | 显示全部楼层
我只是路过打酱油的。
发表于 2019-1-22 09:19:33 | 显示全部楼层
强烈支持楼主ing……
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-4-25 12:11

© 2014-2021

快速回复 返回顶部 返回列表