How can I map a specific input format using java.util.regex in java?

INPUT

Login can be in any of the forms below with the following required content TXT { Any comma separated strings in any format}

String loginURL = "http://ip:port/path?username=abcd&location={LOCATION}&TXT{UE-IP,UE-Username,UE-Password}&password={PASS}";
String loginURL1 = "http://ip:port/path?username=abcd&location={LOCATION}&password={PASS}&TXT{UE-IP,UE-Username,UE-Password}";
String loginURL2 = "http://ip:port/path?TXT{UE-IP,UE-Username,UE-Password}&username=abcd&location={LOCATION}&password={PASS}";
String loginURL3 = "http://ip:port/path?TXT{UE-IP,UE-Username,UE-Password}";
String loginURL4 = "http://ip:port/path?username=abcd&password={PASS}";

Required conclusion

1. OutputURL corresponds to loginURL.

String outputURL = "http://ip:port/path?username=abcd&location={LOCATION}&password={PASS}";
String outputURL1 = "http://ip:port/path?username=abcd&location={LOCATION}&password={PASS}";
String outputURL2 = "http://ip:port/path?username=abcd&location={LOCATION}&password={PASS}";
String outputURL3 = "http://ip:port/path?";
String outputURL4 = "http://ip:port/path?username=abcd&password={PASS}";

2. Remote template (if any)

String deletedPatteren = TXT{UE-IP,UE-Username,UE-Password}

My attempts

String loginURLPattern = TXT+"\\{([\\w-,]*)\\}&*";

System.out.println("1. ");
getListOfTemplates(loginURL, loginURLPattern);
System.out.println();

System.out.println("2. ");
getListOfTemplates(loginURL1, loginURLPattern);
System.out.println();

private static void getListOfTemplates(String inputSequence,String pattern){
    System.out.println("Input URL : " + inputSequence);
    Matcher templateMatcher =  Pattern.compile(pattern).matcher(inputSequence);
    if (templateMatcher.find() && templateMatcher.group(1).length() > 0) {
        System.out.println(templateMatcher.group(1));
        System.out.println("OutputURL : " + templateMatcher.replaceAll(""));
    }
}

EXIT received

1. 
Input URL : http://ip:port/path?username=abcd&location={LOCATION}&TXT{UE-IP,UE-Username,UE-Password}&password={PASS}
UE-IP,UE-Username,UE-Password}&password={PASS
OutputURL : http://ip:port/path?username=abcd&location={LOCATION}&

2. 
Input URL : http://ip:port/path?username=abcd&location={LOCATION}&password={PASS}&TXT{UE-IP,UE-Username,UE-Password}
UE-IP,UE-Username,UE-Password
OutputURL : http://ip:port/path?username=abcd&location={LOCATION}&password={PASS}&

RESULTS OF THE ABOVE SAMPLE

If I add a String containing character like #,%,@ in between TXT{}, then my code will break.

How can I achieve this using the java.util.regex library so that the user can enter any comma-separated String string between TXT{Any Comma Separated Strings}.

+2
source share
1 answer

I would recommend using Matcher.appendReplacement:

public static void main(final String[] args) throws Exception {
    final String[] loginURLs = {
        "http://ip:port/path?username=abcd&location={LOCATION}&TXT{UE-IP,UE-Username,UE-Password}&password={PASS}",
        "http://ip:port/path?username=abcd&location={LOCATION}&password={PASS}&TXT{UE-IP,UE-Username,UE-Password}",
        "http://ip:port/path?TXT{UE-IP,UE-Username,UE-Password}&username=abcd&location={LOCATION}&password={PASS}",
        "http://ip:port/path?TXT{UE-IP,UE-Username,UE-Password}",
        "http://ip:port/path?username=abcd&password={PASS}"};
    final Pattern patt = Pattern.compile("(\\?)?&?(TXT\\{[^}]++})(&)?");
    for (final String loginURL : loginURLs) {
        System.out.printf("%1$-10s %2$s%n", "Processing", loginURL);
        final StringBuffer sb = new StringBuffer();
        final Matcher matcher = patt.matcher(loginURL);
        while (matcher.find()) {
            final String found = matcher.group(2);
            System.out.printf("%1$-10s %2$s%n", "Found", found);
            if (matcher.group(1) != null && matcher.group(3) != null) {
                matcher.appendReplacement(sb, "$1");                
            } else {
                matcher.appendReplacement(sb, "$3");
            }
        }
        matcher.appendTail(sb);
        System.out.printf("%1$-10s %2$s%n%n", "Processed", sb.toString());
    }
}

Output:

Processing http://ip:port/path?username=abcd&location={LOCATION}&TXT{UE-IP,UE-Username,UE-Password}&password={PASS}
Found      TXT{UE-IP,UE-Username,UE-Password}
Processed  http://ip:port/path?username=abcd&location={LOCATION}&password={PASS}

Processing http://ip:port/path?username=abcd&location={LOCATION}&password={PASS}&TXT{UE-IP,UE-Username,UE-Password}
Found      TXT{UE-IP,UE-Username,UE-Password}
Processed  http://ip:port/path?username=abcd&location={LOCATION}&password={PASS}

Processing http://ip:port/path?TXT{UE-IP,UE-Username,UE-Password}&username=abcd&location={LOCATION}&password={PASS}
Found      TXT{UE-IP,UE-Username,UE-Password}
Processed  http://ip:port/path?username=abcd&location={LOCATION}&password={PASS}

Processing http://ip:port/path?TXT{UE-IP,UE-Username,UE-Password}
Found      TXT{UE-IP,UE-Username,UE-Password}
Processed  http://ip:port/path

Processing http://ip:port/path?username=abcd&password={PASS}
Processed  http://ip:port/path?username=abcd&password={PASS}

, 3 :

  • "? {t} &" → "?"
  • "& {t} &" → "&"
  • "? {TEXT}" → ""

, , . :

(\\?)?&?(TXT\\{[^}]++})(&)?

:

  • (\\?)? ?
  • &? &
  • (TXT\\{[^}]++}) TXT, {, } ( ), } (
  • (&)? &

3 :

  • ?
  • &

, , 1..3

if (matcher.group(1) != null && matcher.group(3) != null) {
    matcher.appendReplacement(sb, "$1");                
} else {
    matcher.appendReplacement(sb, "$3");
}

1 3:

1; "?" 1, $1.

2 3:

2 "&" 3 "".
, 2 group 3 "&" 3 ", $3 .

TXT{...}, . , , ? & , String found. {}, .

, Pattern - Matcher, . Pattern, () . static final, - , . - Pattern static final, Matcher .

, Matcher.appendReplacement , , . .

+4

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


All Articles