简体   繁体   中英

Java Regular Expression : Need Simpler Solution

I am new to regular expressions. I have a string named encryptId (does not contain | ) and I want to append the | character after every 20 characters of this string, using the encryptId.replace/replaceAll(Regex,Pattern) function in Java. But it should never have \\ at the end of the string.

Thanks for your help.

EDIT:

  1. The reason I want to use replace and replaceAll functions particularly is because I have to use that in velocity template mananger . And there we can use common String functions but can't write whole java code.

  2. My current solution is shown below

 encryptId = encryptId.replaceAll("(.{20})","$1|"); if(encryptId.charAt(encryptId.length() - 1)=='|') { encryptId = encryptId.substring(0,encryptId.length()-1); } 

I need to get rid of this if statement so that It would be just a string function.

You can use String.toCharArray :

    String s = "..."; //your string
    int i = 0;
    StringBuilder res = new StringBuilder("");
    for (char c : s.toCharArray()){
        res.append(c);
        i++;
        if (i % 20 == 0 && i != s.length()){
            res.append("|");
        }
    }
    System.out.println(res.toString());

res will have your first String with an | every 20 characters but not at the end of the String .

You asked how to do it with replaceAll : I say don't. Regular expressions are not always the best approach to string manipulation problems.

You can efficiently build the new string by taking 20 character blocks from encryptId and appending them to a StringBuilder , optionally appending the pipe if it will not be at the end of the string:

String method(String encryptId) {
  StringBuilder sb = new StringBuilder(encryptId.length() + encryptId.length() / 20);
  for (int i = 0; i < encryptId.length(); i += 20) {
    int end = Math.min(i + 20, encryptId.length());
    sb.append(encryptId, i, end);
    if (end != encryptId.length()) {
      sb.append('|');
    }
  }
  return sb.toString();
}

Regex may complicate things more. You can also try to use StringBuilder for this:

        String encryptId = "test";
        StringBuilder builder = new StringBuilder(encryptId);

        int insertAfter = 20;
        for(int i = encryptId.length(); i > 0 ; i--) {
            if (i % insertAfter == 0 && i != encryptId.length()) {
                builder.insert(i, "|");
            }
        }

This can be done via regular expressions as follows

static String enterADelimiter(String str, String delimiter, int after) {
        String regex = "(.{" + after +"})(?!$)";
        String replacement = "$1" + delimiter;

        return str.replaceAll(regex, replacement);
    }

Just use

enterADelimiter(yourString, "|", 20)

This will return correct solution. Explantion

(            Start group 1
  .          Match Anything
  {after}    after times
)            End group 1
(?!$)        Don't match if at end of String 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM