Regex - Remove everything before first comma and everything after second comma in line

后端 未结 3 1005
孤街浪徒
孤街浪徒 2021-01-17 05:06

I have the following string:

55,1001wuensche.com,0,354137264,1,\"0.00 %\",0,\"0.00 %\",\"2016-04-24 09:00:24\"
56,100hoch3.de,47,2757361,2,\"0.00 %\",0,\"0.0         


        
3条回答
  •  北海茫月
    2021-01-17 05:14

    Another way to do this sort of thing (in a more general sense) is to "split the line by commas, into an array, then take only the second element of that array.

    Yet-another way to do it is to execute two "substitute" regexes, both explicitly anchored to the beginning or to the end of the line (and the first being non-"greedy" e.g.:

    s/^.*\?,//
    
    s/\,.*$//
    

    The concept of "greediness" is quite important, because in the first case we want to match the least number of characters, so as to stop at the first comma that is encountered. (Hence, "non-greedy.") Whereas, in the second case, you do want to "greedily" identify (and set to empty-string) the biggest match that you can find: namely, "the rest of the string."

    Find the simplest and most obvious way to do it, because, quite inevitably, someone's going to want to change this logic someday. Or, someone will hand you a file that breaks your "clever, elegant" approach. Think "testable, and maintainable."

提交回复
热议问题