Does a Regex match a partial or camel rope?

I would like the regular expression to match the given partial or camel string. For example, if the search set contains the string "MyPossibleResultString", I want to be able to match it as follows:

  • MyPossibleResultString
  • MPRS
  • MPRString
  • MyPosResStr
  • M

I would also like to include wildcards, for example:

  • Myp * rstring
  • * PosResString
  • My * String

If it's not clear what I mean, the only example I can think of is the Eclipse Open Type dialog, which pretty much matches the exact behavior I'm looking for. I don't use regular expressions too much, so I'm not sure if it matters if I'm looking for a solution in Java.

+3
5

, , , , . , . :

String re = "\\b(" + query.replaceAll("([A-Z][^A-Z]*)", "$1[^A-Z]*") + ".*?)\\b";

, MyPosResStr :

\\b(My[^A-Z]*Pos[^A-Z]*Res[^A-Z]*Str[^A-Z]*.*?)\\b

, Matcher.find, - :

public static String matchCamelCase(String query, String str) {
    query = query.replaceAll("\\*", ".*?");
    String re = "\\b(" + query.replaceAll("([A-Z][^A-Z]*)", "$1[^A-Z]*") + ".*?)\\b";

    System.out.println(re);
    Pattern regex = Pattern.compile(re);

    Matcher m = regex.matcher(str);

    if  (m.find()) {
        return m.group();
    } else return null;
}

str.

EDIT: , .

+4

danbruc, . , .

public Pattern queryToPattern(String query) {
    StringBuilder sb = new StringBuilder();
    char[] chars = query.toCharArray();
    boolean incamel = false;
    for (int i=0; i < chars.length; i++) {
        if (chars[i] == '*') {
                            if (!incamel)
                    sb.append(".*");
        } else if (Character.isUpperCase(chars[i])) {
            if (incamel) {
                sb.append(".*");
            }
            sb.append(chars[i]);
            incamel = true;
        } else {
            sb.append(chars[i]);
        }

    }
    sb.append(".*");
    return Pattern.compile(sb.toString());
}

: MyP * RString

: My. * P. * R. * String. *

+2

. . , - , (cammel cased), .

+1

- :

class RegexTransformer {
    public String fromQuery(String query) {
        StringBuilder sb = new StringBuilder();
        sb.append("^");
        sb.append(query.replaceAll("(\\p{Upper}(?!\\p{Lower}))",
                "$1\\\\p{Alpha}*?"));
        sb.append("$");
        return sb.toString();
    }
}

. API- (?!pat), POSIX \p{class} *?.

:

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

public class RegexTransformerTest {

    private RegexTransformer rt = new RegexTransformer();

    @Test
    public void testQueries() {
        String in = "MyPossibleResultString";
        String q1 = "MyPossibleResultString";
        String q2 = "MPRS";
        String q3 = "MPRString";
        String q4 = "MyPosResStr"; // this wont work
        String q5 = "M";

        test(in, q1, "^MyPossibleResultString$");
        test(in, q2, "^M\\p{Alpha}*?P\\p{Alpha}*?R\\p{Alpha}*?S\\p{Alpha}*?$");
        test(in, q3, "^M\\p{Alpha}*?P\\p{Alpha}*?R\\p{Alpha}*?String$");
        test(in, q5, "^M\\p{Alpha}*?$");
    }

    private void test(String in, String query, String expected) {
        assertEquals("transform", expected, rt.fromQuery(query));
        assertTrue("match", in.matches(rt.fromQuery(query)));
    }
}
0

Il-Bhima , , ( #, ):

pattern = Regex.Escape(pattern);
pattern = pattern.Replace(@"\*", ".*?");
pattern = Regex.Replace(pattern, "([A-Z][^A-Z]*)", "$1[^A-Z]*?") + ".*";

Pay attention to the “. *” At the end, which allows incomplete “starting” phrases (also allows you not to specify all uppercase letters). In addition, an asterisk after the “[^ AZ] *” server fixes problems such as q4 in response to the toolkit , where small letters were provided after capital (they should appear immediately after the capital letter, and not before the next).

0
source

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


All Articles