2

Given a string like that:

"CSV;XML;ZIP"

I'm trying to make a regex to transform it to something like that:

".csv,.xml,.zip"

I tried this regex pattern : ([^;]*)(;|$) replace with: .\L$1,

but the result is : .csv,.xml,.zip,.,

I need my regex to satisfy :

  1. "CSV" -> ".csv"
  2. "CSV;XML;ZIP" -> ".csv,.xml,.zip"

I feel like I need to add something inside my regex which says: if it is the end of the string don't add ',' else do, but I don't know how to do it.

6
  • 3
    Case modifying operators are not supported in the replacement patterns in Java replace regex methods. If you are not "married" to regex, 1) lowercase the input, 2) split with ; and 3) join with ,. and append . at the start. Mar 12, 2021 at 11:46
  • Try using StringJoiner. Example: docs.oracle.com/javase/8/docs/api/java/util/StringJoiner.html Mar 12, 2021 at 11:49
  • BTW, regarding I feel like I need to add somthing inside my regex wich says : if it is the end of the string don't add ',' else do, but I don't know how to do it. - you just need +, not *. ([^;]+)(;|$) Mar 12, 2021 at 11:55
  • @WiktorStribiżew thank you okey, I thought regex was the "cleanest" way to do that, but if it is not I'll use string join soluiton, BTW your solution ([^;]+)(;|$) gives -> .csv,.xml,.zip, so there is a "," at the end and it shouldn't Mar 12, 2021 at 12:01
  • 1
    Yes, that is why Pshemo mentions you need a conditional replacement, again, this is not supported in Java regex methods. (Conditional replacements are available in Boost and PCRE2) Mar 12, 2021 at 12:04

2 Answers 2

4

-- This is just another way to do it, that you can think about...

Like someone else already menitoned, in this case, you should think about going trough it with a loop. Split the Input by semicolon and process through it..

I'm using the Stream API in my solution

  1. lowercase the string
  2. Split the string by semicolon and create a stream out of the substrings
  3. Map toLowerCase
  4. Map ("." + element)
  5. Collect with StringJoiner - use comma as seperator
        String input = "CSV;ZIP;XML";
        String output = Arrays.stream(input.toLowerCase().split(";"))
                .map(s -> "." + s)
                .collect(Collectors.joining(","));

This solution works and in my opinion is much more readable than a regex

2
  • 1
    Thats a good point, i've could just lowerCased the input itself, true Mar 12, 2021 at 12:10
  • You are welcome. Note that you only updated your code, not the explanations. Yeah, "comments lie", all the time ;-)
    – GhostCat
    Mar 12, 2021 at 12:25
3

Demo:

String str = "CSV;XML;ZIP";

Pattern p = Pattern.compile("([^;]+)(;|$)");
Matcher m = p.matcher(str);

String replaced = m.replaceAll(
        match ->
        "." //in all cases replacement will start with dot
        + match.group(1).toLowerCase() //then lowercase ([^;]+)
        + (match.group(2).isEmpty() ? "" : ",") //then depending on (;|$) we decide if we add comma or not
);
System.out.println(replaced);

Output: .csv,.xml,.zip

2
  • A good example why regexes are sometimes just the wrong approach. "Look here, I have a screw, so lets use a hammer to force it into the wall". Yeah, sure, works. But is it what you should be doing?!
    – GhostCat
    Mar 12, 2021 at 12:07
  • 1
    @GhostCat yeah i agree, but at the begining i thought that it is a really simple regex that anyone who has decent knowlege of regex could easly do, and (also it's my own junior dev opinion) if you could do it with one shot regex than do it with regex because it will be more efficient than a loop, but in this case it turned out that it is impossible to do it with a one shot regex so i agree that the best solution would be the one with string.split.join Mar 12, 2021 at 12:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.