1-liners


  • 10

    This problem begs for a regular expression solution...

    Ruby:

    def remove_comments(source)
      source.join($/).gsub(%r(//.*|/\*(.|\n)*?\*/), '').split($/).reject(&:empty?)
    end
    

    Python:

    def removeComments(self, source):
        return filter(None, re.sub('//.*|/\*(.|\n)*?\*/', '', '\n'.join(source)).split('\n'))

  • 3
    M

    In Java:

    public List<String> removeComments(String[] source) {
        String[] s = String.join("\n",source).replaceAll("//.*|/\\*(.|\n)*?\\*/", "").split("\n");
        List<String> ans = new ArrayList<>();
        for(String str : s) if(!str.equals("")) ans.add(str);
        return ans;
    }
    

  • 7
    L

    Explanation of regex in details, hope it will be helpful for regex beginner like me...

    //.*     # matches single line comments
    |        # 'or' operator
    /\*      # matches '\*',
    (.|\n)*? # matches everything(including '\n') inside block comment 
             # question mark will make regex '(.|\n)*' non-greedy or lazy
    \*/      # matches '*/'
    
    re.sub('//.*|/\*(.|\n)*?\*/', '', inputString) # replace matched result in the inputString with empty string.
    

    Notes: The difference between (.*) and (.*?) is that the latter one is non-greedy regex. For example, given a inputString = '/*comment*/NotComment*/',
    '/\*(.*)\*/' will match comment*/NotComment in the brackets.
    '/\*(.*?)\*/' will match comment lazily


  • 0
    M

    (.*) is greedy and will match as many as possible.
    (.*?) is lazy and will match as less as possible.

    For examaple, input = ["12/34/56*/78"],
    (.*) returns ["1278"]
    (.*?) return ["1256*/78"], which is correct.

    For more info about the difference between ".*" and ".*?", please refer to https://stackoverflow.com/questions/3075130/.


  • 0
    C

    @moto72 said in 1-liners:

    String[] s = String.join("\n",source).replaceAll("//.|/\(.|\n)?\/", "").split("\n");

    for this line, why not the first part //.* replace all the rest of joined string, but stop at \n?
    like abcd//abc\nabc will return abcd\nabc?
    Can anyone help?


  • 0
    C

    @cathead said in 1-liners:

    @moto72 said in 1-liners:

    String[] s = String.join("\n",source).replaceAll("//.|/\(.|\n)?\/", "").split("\n");

    for this line, why not the first part //.* replace all the rest of joined string, but stop at \n?
    like abcd//abc\nabc will return abcd\nabc?
    Can anyone help?

    I found answer here.
    https://stackoverflow.com/questions/18865393/java-replaceall-not-working-for-n-characters


  • 0
    M

    @cathead It is because "." matches any single character except newline character. ("\n")


  • 0
    C

    @moto72 Thank you moto72


  • 0
    C

    @StefanPochmann
    Why there (%r(//.|/*(.|\n)?*/), '') use ? but not {1}?


  • 0
    T

    @StefanPochmann said in 1-liners:

    '\n'.join(source)).split('\n')

    why use '\n'.join(source)).split('\n'),
    is there any difference if I just use source?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.