How to find a list of email addresses for a specific format?

I'm looking for a regex that can help me find a list of lines of e-mail, for example, if I have arraylista few that you specify e-mails, for example firstname.lastname@company.com, firstname1.lastname1@company.com I would like to find them in my filter, if I add rst name1, it will show firstname1.lastname1@company.com, I will have the filter code is in place and it will search for each matched letter. however, I would like to change it and make it look for the characters before or after the dot ".". with regular expressions. How can I do it? Here is my filter search code:

protected synchronized FilterResults performFiltering(CharSequence constraint) {

                FilterResults results = new FilterResults();

                if (constraint != null && constraint.length() > 0) {

                    ArrayList<Integer> filterList = new ArrayList<>();

                    int iCnt = listItemsHolder.Names.size();
                    for (int i = 0; i < iCnt; i++) {
                        if(listItemsHolder.Types.get(i).toString().indexOf("HEADER_")>-1){
                            continue;
                        }
                        if (listItemsHolder.Names.get(i).toLowerCase().contains(constraint.toString().toLowerCase())) {
                            if(filterList.contains(i))
                                continue;

                            filterList.add(i);

                        }

                    }

                    results.count = filterList.size();

                    results.values = filterList;
                }else {

                    results.count = listItemsHolder.Names.size();

                    ArrayList<Integer> tList = new ArrayList<>();
                    for(int i=0;i<results.count;i++){
                        tList.add(i);
                    }

                    results.values = tList;

                }

                return results;
            }


            //Invoked in the UI thread to publish the filtering results in the user interface.
            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                ArrayList<Integer> resultsList = (ArrayList<Integer>)results.values;
                if(resultsList != null) {
                    m_filterList = resultsList;
                }
                notifyDataSetChanged();
            }
        }

Where

class ListItemsHolder {

public ArrayList<String>        Names;
}

contains all the necessary names in the format firstname.lastname@company.com

+4
3

, , , ( char ) , ( ).

REGEX:

, , - :

boolean itFit; //additional variable
List<String> results;

if (constraint != null && constraint.length() > 0) {

    ArrayList<Integer> filterList = new ArrayList<>();

    int iCnt = listItemsHolder.Names.size();
    for (int i = 0; i < iCnt; i++) {
        itFit = true;     // default state of itFit is ture,
        if(listItemsHolder.Types.get(i).toString().indexOf("HEADER_")>-1){
            continue;
        }
        for(String con : constraint.toString().split("\\s")) {   // constraint as string is splitted with space (\s) as delimiter, the result is an array with at least one element (if there is no space)
            if (!listItemsHolder.Names.get(i).toLowerCase().contains(con.toLowerCase())) {
                itFit = false;    // if email doesn't contains any part of splitted constraint, the itFit value is changed to false 
            }

        }
        if(itFit && !filterList.contains(i)){   // if itFit is true, add element to list
            filterList.add(listItemsHolder.Names.get(i));
        }
    }

    results = filterList;
}

List<String>, , . , , -. .

REGEX:

:

public String getRegEx(CharSequence elements){
    String result = "(?i).*";
    for(String element : elements.toString().split("\\s")){
        result += element + ".*";
    }
    result += "@.*";  // it will return String like "(?i).*j.*bau.*"
    return result;
}

:

if (listItemsHolder.Names.get(i).toLowerCase().contains(constraint.toString().toLowerCase())) {

if (listItemsHolder.Names.get(i).matches(getRegEx(constraint))) {

\., , j bau jeff.bauser@company.com jerbauman@comp.com. , , , , , , . f name l name, firstname1.lastname1@company.com.

+2

, .*\..*

, jer ld jer.*\..*ld "jerry.seinfeld" -

, "jerald.melberg" - "jer" "ld" ".". - , ?

- " " (.*), (\.), (.*) "

:

// just once before the loop:
String regex = constraint.replaceAll("[ .]+", ".*\..*")+".*@";
Pattern pattern = Pattern.compile(regex, CASE_INSENSITIVE);

// then in the loop,
// instead of:
// if (listItemsHolder.Names.get(i).toLowerCase().contains(constraint.toString().toLowerCase())) {
// do instead:
if (pattern.matcher(listItemsHolder.Names.get(i)).matches()) { ...
+1

, .

, , " 1" "firstname1.lastname1@company.com". Regex, :

.*rst.*\..*name1.*\@

:

regex:   .*   rst   .*    \.  .*   ast   .*     \@
matches: fi   rst   name  .   l    ast   name   @

: "rst", :

.*rst.*\@

( ):

arrayOfConstraintParts = constraint.split(" ");  // explode the constraint into an array of constraints
if (arrayOfConstraintParts.length >= 2) {        // If you have at least 2 constraints
    regex = ".*" . arrayOfConstraintParts[0] . ".*\..*" . arrayOfConstraintParts[1] . ".*\@";
} else {                                         // If you have only 1 constraint
    regex = ".*" . arrayOfConstraintParts[0] . ".*\@";
}

: https://regex101.com/

0

Source: https://habr.com/ru/post/1612822/


All Articles