Example usage for java.util.regex Pattern matches

List of usage examples for java.util.regex Pattern matches

Introduction

In this page you can find the example usage for java.util.regex Pattern matches.

Prototype

public static boolean matches(String regex, CharSequence input) 

Source Link

Document

Compiles the given regular expression and attempts to match the given input against it.

Usage

From source file:net.spfbl.whois.Owner.java

/**
 * Verifica se a expresso  um CPF./*from   w ww.java 2 s  .c o m*/
 * @param id a identificao a ser verificada.
 * @return verdadeiro se a expresso  um CPF.
 */
public static boolean isOwnerCPF(String id) {
    return Pattern.matches("^([0-9]{3}\\.[0-9]{3}\\.[0-9]{3}-[0-9]{2})$", id);
}

From source file:org.ahanna.DoubleConversionMapper.java

public void fixATweet(JSONObject jsonObj) {
    if (jsonObj == null)
        return;//from w  ww . java2s .c o m
    String str = "";
    if (jsonObj.has("created_at")) {
        str = (String) jsonObj.get("created_at");
        if (!str.equals("null") && !Pattern.matches(sqlDateRegex, str)) {
            String[] splits = str.split(" ");
            String time = splits[5] + " " + splits[1] + " " + splits[2] + " " + splits[3];
            SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy MMM dd HH:mm:ss");
            Date myParsedDate = null;

            try {
                myParsedDate = timeFormat.parse(time);
            } catch (ParseException e) {
                // nope
            }
            if (myParsedDate != null) {
                timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                jsonObj.put("created_at", timeFormat.format(myParsedDate));
            }
        }
    }
    /** Get Coordinates within Geo fixed**/
    if (jsonObj.has("geo")) {
        str = (String) jsonObj.get("geo").toString();
        if (!str.equals("null")) {
            //System.out.println(str);
            JSONObject jobjGeo = (JSONObject) jsonObj.get("geo");
            if (jobjGeo.has("coordinates")) {
                str = (String) jobjGeo.get("coordinates").toString();
                //System.out.println(str);
                if (!str.equals("null")) {
                    JSONArray jobj_geo_coordinates = (JSONArray) jobjGeo.get("coordinates");
                    for (int i = 0; i < jobj_geo_coordinates.length(); i++) {
                        //System.out.println(jobj_geo_coordinates.toString());
                        // if (jobj_geo_coordinates.get(i) instanceof Integer) {
                        if (Pattern.matches(fpRegex, jobj_geo_coordinates.get(i).toString())) {
                            jobj_geo_coordinates.put(i, Double.valueOf(jobj_geo_coordinates.get(i).toString()));
                            //System.out.println("GEO: " + jobj_geo_coordinates.toString());
                        }
                    }
                }
            }
        }
    }
    /** Get Coordinates within Coordinates fixed**/
    if (jsonObj.has("coordinates")) {
        str = (String) jsonObj.get("coordinates").toString();
        if (!str.equals("null")) {
            //System.out.println(str);
            JSONObject jobjCoordinates = (JSONObject) jsonObj.get("coordinates");
            str = (String) jobjCoordinates.get("coordinates").toString();
            //System.out.println(str);
            if (!str.equals("null")) {
                if (jobjCoordinates.has("coordinates")) {
                    JSONArray jobj_coord_coordinates = (JSONArray) jobjCoordinates.get("coordinates");
                    for (int i = 0; i < jobj_coord_coordinates.length(); i++) {
                        //System.out.println(jobj_coord_coordinates.toString());
                        //if (jobj_coord_coordinates.get(i) instanceof Integer) {
                        //System.out.println("Coordinates: " + jobj_coord_coordinates.toString());                            
                        if (Pattern.matches(fpRegex, jobj_coord_coordinates.get(i).toString())) {
                            jobj_coord_coordinates.put(i,
                                    Double.valueOf(jobj_coord_coordinates.get(i).toString()));
                        }
                    }
                }
            }
        }
    }
    /** Get Coordinates within place fixed**/
    if (jsonObj.has("place")) {
        str = (String) jsonObj.get("place").toString();
        if (!str.equals("null")) {
            //System.out.println(jsonObj.get("place"));
            JSONObject jobjPlace = (JSONObject) jsonObj.get("place");
            if (jobjPlace != null && jobjPlace.has("bounding_box")) {
                str = (String) jobjPlace.get("bounding_box").toString();
                //System.out.println(str);
                if (!str.equals("null")) {
                    JSONObject jobjBB = (JSONObject) jobjPlace.get("bounding_box");
                    if (jobjBB != null && jobjBB.has("coordinates")) {
                        str = (String) jobjBB.get("coordinates").toString();
                        //System.out.println(str);
                        JSONArray jobCoordinates_0 = (JSONArray) jobjBB.get("coordinates");
                        for (int i = 0; i < jobCoordinates_0.length(); i++) {
                            JSONArray jobCoordinates_1 = jobCoordinates_0.getJSONArray(i);
                            for (int j = 0; j < jobCoordinates_1.length(); j++) {
                                JSONArray jobCoordinates_2 = jobCoordinates_1.getJSONArray(j);
                                for (int k = 0; k < jobCoordinates_2.length(); k++) {
                                    //if (jobCoordinates_2.get(k) instanceof Integer) {
                                    if (Pattern.matches(fpRegex, jobCoordinates_2.get(k).toString())) {
                                        jobCoordinates_2.put(k,
                                                Double.valueOf(jobCoordinates_2.get(k).toString()));
                                        //System.out.println("PLACE: " + jobCoordinates_2.toString());
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    } //end if
}

From source file:com.cyclopsgroup.waterview.core.DefaultModuleManager.java

/**
 * Overwrite or implement method createDynaView()
 *
 * @see com.cyclopsgroup.waterview.spi.ModuleManager#createDynaView(java.lang.String)
 *///from w  ww.j a  v a  2 s . com
public View createDynaView(String viewPath) throws Exception {
    DynaViewFactory viewFactory = null;
    for (Iterator i = dynaViewFactories.keySet().iterator(); i.hasNext();) {
        String pattern = (String) i.next();
        if (Pattern.matches('^' + pattern + '$', viewPath)) {
            viewFactory = (DynaViewFactory) dynaViewFactories.get(pattern);
            break;
        }
    }
    if (viewFactory == null) {
        return View.DUMMY;
    }
    Path path = parsePath(viewPath);
    View view = viewFactory.createView(path);
    return view == null ? View.DUMMY : view;
}

From source file:it.greenvulcano.util.txt.TextUtils.java

/**
 * Simply calls/* ww  w . jav  a2 s .  c om*/
 * {@link java.util.regex.Pattern#matches(String, CharSequence)}.
 * 
 * @param pattern
 * @param text
 * @return
 */
public static boolean matches(String pattern, String text) {
    return Pattern.matches(pattern, text);
}

From source file:com.norconex.importer.handler.tagger.impl.DeleteTagger.java

private boolean mustDelete(String metaField) {
    // Check with exact field names
    for (String fieldToRemove : fieldsToRemove) {
        if (fieldToRemove.trim().equalsIgnoreCase(metaField.trim())) {
            return true;
        }/*from   w w  w.ja  v  a2 s  .com*/
    }

    // Check with regex
    if (StringUtils.isNotBlank(fieldsRegex) && Pattern.matches(fieldsRegex, metaField)) {
        return true;
    }
    return false;
}

From source file:ambroafb.general.AnnotiationUtils.java

private static boolean checkValidationForContentMailAnnotation(Field field, Object currSceneController) {
    boolean contentIsCorrect = false;
    Annotations.ContentMail annotation = field.getAnnotation(Annotations.ContentMail.class);

    Object[] typeAndContent = getNodesTypeAndContent(field, currSceneController);

    boolean validSyntax = Pattern.matches(annotation.valueForSyntax(), (String) typeAndContent[1]);
    boolean validAlphabet = Pattern.matches(annotation.valueForAlphabet(), (String) typeAndContent[1]);
    boolean predicateValue = getPredicateValue(annotation.predicate(), field.getName());
    if (predicateValue && !validSyntax) { // otherwise email check label may stay a red color
        changeNodeTitleLabelVisual((Node) typeAndContent[0], annotation.explainForSyntax());
    } else if (predicateValue && !validAlphabet) { // otherwise email check label may stay a red color
        changeNodeTitleLabelVisual((Node) typeAndContent[0], annotation.explainForAlphabet());
    } else {/*  w  w  w  .j a  v a 2s .  com*/
        changeNodeTitleLabelVisual((Node) typeAndContent[0], "");
        contentIsCorrect = true;
    }
    return contentIsCorrect;
}

From source file:ambroafb.general.mapeditor.MapEditor.java

public MapEditor() {
    this.setEditable(true);
    itemsMap = new HashMap<>();
    delimiter = " : "; // default value of delimiter
    keyPattern = ""; // (?<![\\d-])\\d+
    valuePattern = ""; // [0-9]{1,13}(\\.[0-9]*)?
    keySpecChars = "";
    valueSpecChars = "";

    this.setCellFactory((ListView<MapEditorElement> param) -> new CustomCell());

    removeElement = (MapEditorElement elem) -> {
        if (itemsMap.containsKey(elem.getKey())) {
            itemsMap.remove(elem.getKey());
            if (getValue() != null && getValue().compare(elem) == 0) {
                getEditor().setText(delimiter);
            }/*  w  w w.ja  v  a 2  s . co m*/
            getItems().remove(elem);
        }
    };

    editElement = (MapEditorElement elem) -> {
        getSelectionModel().select(-1);
        getEditor().setText(elem.getKey() + delimiter + elem.getValue());
        itemsMap.remove(elem.getKey());
        getItems().remove(elem);
    };

    // Never hide comboBox items listView:
    this.setSkin(new ComboBoxListViewSkin(this) {
        @Override
        protected boolean isHideOnClickEnabled() {
            return false;
        }
    });

    // Control textField input.
    TextField editor = getEditor();
    editor.setText(delimiter);
    editor.textProperty()
            .addListener((ObservableValue<? extends String> observable, String oldValue, String newValue) -> {
                if (newValue == null || newValue.isEmpty() || newValue.equals(delimiter)) {
                    editor.setText(delimiter);
                } else if (!newValue.contains(delimiter)) {
                    editor.setText(oldValue);
                } else {
                    String keyInput = StringUtils.substringBefore(newValue, delimiter).trim();
                    String valueInput = StringUtils.substringAfter(newValue, delimiter).trim();

                    if (!keyInput.isEmpty() && !Pattern.matches(keyPattern, keyInput)) {
                        keyInput = StringUtils.substringBefore(oldValue, delimiter).trim();
                    }
                    if (!valueInput.isEmpty() && !Pattern.matches(valuePattern, valueInput)) {
                        valueInput = StringUtils.substringAfter(oldValue, delimiter).trim();
                    }

                    editor.setText(keyInput + delimiter + valueInput);
                }
            });

    this.setConverter(new StringConverter<MapEditorElement>() {
        @Override
        public String toString(MapEditorElement object) {
            if (object == null) {
                return delimiter;
            }
            return object.getKey() + delimiter + object.getValue();
        }

        @Override
        public MapEditorElement fromString(String input) {
            MapEditorElement result = null;
            if (input != null && input.contains(delimiter)) {
                result = getNewInstance();
                if (result == null)
                    return null;
                String keyInput = StringUtils.substringBefore(input, delimiter).trim();
                String valueInput = StringUtils.substringAfter(input, delimiter).trim();
                if (!keyInput.isEmpty()) {
                    result.setKey(keyInput);
                }
                if (!valueInput.isEmpty()) {
                    result.setValue(valueInput);
                }
                boolean keyOutOfSpec = keySpecChars.isEmpty()
                        || !StringUtils.containsOnly(result.getKey(), keySpecChars);
                boolean valueOutOfSpec = valueSpecChars.isEmpty()
                        || !StringUtils.containsOnly(result.getValue(), valueSpecChars);
                if (!keyInput.isEmpty() && !valueInput.isEmpty() && !itemsMap.containsKey(keyInput)
                        && (keyOutOfSpec && valueOutOfSpec)) {
                    itemsMap.put(keyInput, result);
                    getItems().add(result);
                    return null;
                }
            }
            return result;
        }
    });

    // Control caret position in textField.
    editor.addEventFilter(KeyEvent.KEY_PRESSED, (KeyEvent event) -> {
        int caretOldPos = editor.getCaretPosition();
        int delimiterIndex = editor.getText().indexOf(delimiter);
        if (event.getCode().equals(KeyCode.RIGHT)) {
            if (caretOldPos + 1 > delimiterIndex && caretOldPos + 1 <= delimiterIndex + delimiter.length()) {
                editor.positionCaret(delimiterIndex + delimiter.length());
                event.consume();
            }
        } else if (event.getCode().equals(KeyCode.LEFT)) {
            if (caretOldPos - 1 >= delimiterIndex && caretOldPos - 1 < delimiterIndex + delimiter.length()) {
                editor.positionCaret(delimiterIndex);
                event.consume();
            }
        }
    });
}

From source file:com.wyb.utils.util.PatternUtil.java

/**
 * ?//from  ww w.j av a 2s .  com
 *
 * @param decimals ??0-91.23233.30
 * @return ??true?false
 */
public static boolean isDecimals(String decimals) {
    if (StringUtils.isBlank(decimals)) {
        return false;
    }
    String regex = "\\-?[1-9]\\d+(\\.\\d+)?";
    return Pattern.matches(regex, decimals);
}

From source file:com.u2apple.tool.filter.ExcludeFilter.java

private boolean valueMatches(String operator, String prop, String value) {
    boolean matches = false;
    if (null != operator)
        switch (operator) {
        case "equals":
            matches = prop.equals(value);
            break;
        case "equalsIgnoreCase":
            matches = prop.equalsIgnoreCase(value);
            break;
        case "regexp":
            matches = Pattern.matches(value, prop);
            break;
        }//from ww  w .j  av  a  2s . c om
    return matches;
}

From source file:com.cyclopsgroup.waterview.valves.ResolveActionsValve.java

/**
 * Override or implement method of parent class or interface
 *
 * @see com.cyclopsgroup.waterview.Valve#invoke(com.cyclopsgroup.waterview.PageRuntime, com.cyclopsgroup.waterview.PipelineContext)
 *//*ww  w. jav a  2 s. c  om*/
public void invoke(PageRuntime runtime, PipelineContext context) throws Exception {
    for (Iterator i = runtime.getActions().iterator(); i.hasNext();) {
        String actionName = (String) i.next();
        for (Iterator j = actionResolvers.keySet().iterator(); j.hasNext();) {
            String pattern = (String) j.next();
            if (Pattern.matches('^' + pattern + '$', actionName)) {
                ActionResolver resolver = (ActionResolver) actionResolvers.get(pattern);
                resolver.resolveAction(actionName, runtime);
                break;
            }
        }
    }
    context.invokeNextValve(runtime);
}