Split Ruby regex over multiple lines

后端 未结 4 1631
旧巷少年郎
旧巷少年郎 2020-12-13 11:48

This might not be quite the question you\'re expecting! I don\'t want a regex that will match over line-breaks; instead, I want to write a long regex that, for readability,

4条回答
  •  佛祖请我去吃肉
    2020-12-13 12:31

    Rather than cutting the regex mid-expression, I suggest breaking it into parts:

    full_rgx = /This is a message\. A phone number: \d{10}\. A timestamp: \d*?/
    
    msg = /This is a message\./
    phone = /A phone number: \d{10}\./
    tstamp = /A timestamp: \d*?/
    
    /#{msg} #{phone} #{tstamp}/
    

    I do the same for long strings.

提交回复
热议问题