采用Android Pattern和Matcher类(正则表达式) [英] Using Android Pattern and Matcher class (Regex)

查看:350
本文介绍了采用Android Pattern和Matcher类(正则表达式)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚拿起的Andr​​oid,但我任务是帮助我的实习项目。

I have just picked up Android, but am tasked to help with a project for my internship.

可以说我有下面的详细信息:

Lets say I have the details below:

Fonia Taylo
Product Manager

foniataylo@gmail.com
98706886

从我上面的细节,我想将它传递到一个类,由此我就可以使用正则表达式过滤掉的电子邮件地址,并通过此过滤掉的电子邮件地址,以一个EditText。

From the details I have above, I want to pass it into a class whereby I can then filter out the email address using regex, and pass only this filtered out email address to an EditText.

我搜索过的正则表达式很多教程,特别是在Android Pattern和Matcher类。

I have searched many tutorials on regex, especially on Android Pattern and Matcher classes.

但我发现所有的例子只是用于验证了在文本输入到只有一个EditText字段。

But all the examples I have found are only for validation for the text entered into an EditText field only.

我需要做的是:


  1. 验证整个文本如上图

  2. 过滤出使用正则表达式的电子邮件地址(并删除文本的其余部分)

  3. 此电子邮件地址,以一个EditText

  1. Validate the entire text as shown above
  2. Filter out the email address using the regex (and delete the rest of the text)
  3. Pass only this email address to an EditText

目前下面是我的类:

public class RegexOCR1 extends Activity {

    private Pattern pattern;
    private Matcher matcher;

    private String recognizedText, textToUse;

    private static final String EMAIL_PATTERN =
            "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
                    + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_createcontact);

        // Getting the path of the image from another class
        Bundle extras = this.getIntent().getExtras();
        recognizedText = extras.getString("TEXT");
        textToUse = recognizedText;

    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        setContentView(R.layout.usetext);
        showText();
        //Log.i(TAG, "onConfigChanged");
    }

    private void showText(){
        //Log.i(TAG, "ShowText");
        Intent intent = new Intent(this, CreateContactActivityOCR.class);
        startActivity(intent);
    }

    public EmailValidator() {
    Pattern pattern = Pattern.compile(EMAIL_PATTERN);
    Matcher matcher = pattern.matcher(textToUse);
    if (matcher.find())
    {
        String email = textToUse.substring(matcher.start(), matcher.end());


    } else {
        // TODO handle condition when input doesn't have an email address
    }
    }

    public boolean validate(final String hex) {

        matcher = pattern.matcher(hex);
        return matcher.matches();

    }
}

正如你所看到的,它是pretty很多不完整的。我想通过textToUse进入正则表达式验证,然后继续执行的功能如上所述。

As you can see, it is pretty much incomplete. I would like to pass "textToUse" into the regex validation, and then continue to perform the function as stated above.

编辑:

下面的方法后:

public EmailValidator() {
        Pattern pattern = Pattern.compile(EMAIL_PATTERN);
        Matcher matcher = pattern.matcher(textToUse);
        if (matcher.find())
        {
            String email = textToUse.substring(matcher.start(), matcher.end());


        } else {
            // TODO handle condition when input doesn't have an email address
        }
        }

其中解压出来的电子邮件地址

我怎么那么通过这个提取到通过意图电子邮件地址的的EditText 是在其他类

which extract out the email address; How do I then pass this extracted email address to through intent to an EditText that is in another Class?

请让我知道你,我怎么可以改变我的code,如果有任何想法。谢谢!

Please let me know you how I can change my code if there are any ideas. Thank you!

推荐答案

在Android SDK中,有一个名为类 android.util.Patterns ,在这里您可以找到各种有用的正则表达式模式。

In Android SDK, there is class called android.util.Patterns, in which you can find various useful regex patterns.


  • 电子邮件地址:

  • E-Mail Address:

android.util.Patterns.EMAIL_ADDRESS


然后,你可以简单地使用它们像这样:

Then you can simply use them like this:

String target = "";

if(android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches()){

}

这篇关于采用Android Pattern和Matcher类(正则表达式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆