How to convert "string" to "* s * t * r * i * n * g *"

I need to convert a string like

"string" 

to

 "*s*t*r*i*n*g*" 

What is a regex pattern? Language is Java.

+4
source share
4 answers

You want to match an empty string and replace it with "*" . So something like this works:

 System.out.println("string".replaceAll("", "*")); // "*s*t*r*i*n*g*" 

Or even better, since an empty string can be matched literally without a regular expression, you can simply do:

 System.out.println("string".replace("", "*")); // "*s*t*r*i*n*g*" 

Why does it work

This is because any instance of the string startsWith("") and endsWith("") and contains(""). Between any two characters in any line there is an empty line. In fact, there are an infinite number of empty lines in these places.

(And yes, this is true for the empty line itself. This is the "empty" line contains itself!).

The regex mechanism and String.replace automatically advance the index when looking for the next match in these cases, to prevent an infinite loop.


"Real" regex

There is no need for this, but it is shown here for educational purposes: something like this also works:

 System.out.println("string".replaceAll(".?", "*$0")); // "*s*t*r*i*n*g*" 

This works by matching "any" character with . and replacing it with * and this character, by calling back to group 0.

To add an asterisk for the last character, we allow . be optional with .? . Does it work because ? is greedy and always accepts a character, if possible, i.e. Anywhere except the last character.

If the string can contain newline characters, use Pattern.DOTALL/(?s) mode.

References

+23
source

I think "" is the regular expression you want.

 System.out.println("string".replaceAll("", "*")); 

Prints *s*t*r*i*n*g* .

+5
source

If that's all you do, I would not use a regex:

 public static String glitzItUp(String text) { return insertPeriodically(text, "*", 1); } 

Putting char in a java string for every N characters

 public static String insertPeriodically( String text, String insert, int period) { StringBuilder builder = new StringBuilder( text.length() + insert.length() * (text.length()/period)+1); int index = 0; while (index <= text.length()) { builder.append(insert); builder.append(text.substring(index, Math.min(index + period, text.length()))); index += period; } return builder.toString(); } 

Another advantage (besides simplicity) is that it is ten times faster than a regular expression.

IDEOne | Working example

+3
source

Just to be a jerk, I'm going to say that J:

I spent the school year learning Java, and myself participated in J for some time during the summer, and if you are going to do this for yourself, then it is probably most productive to use J simply because all this insertion of an asterisk object is easily done with one simple verb definitions using one cycle.

 asterisked =: 3 : 0 i =. 0 running_String =. '*' while. i < #y do. NB. #y returns tally, or number of items in y: right operand to the verb running_String =. running_String, (i{y) , '*' i =. >: i end. ]running_String ) 

That's why I would use J: I know how to do this, and studied the language for a couple of months fluently. It's not as concise as the whole .replaceAll () method, but you can do it yourself and easily edit it in your specifications later. Feel free to remove this / troll, this / get inflammation at my suggestion J, I really don't care: I don't advertise it.

0
source

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


All Articles