首先,我对正则表达式的了解不是很好。我个人将它们与哈利波特书中的伏地魔进行比较。
我试图使用正则表达式将一个相当大的字符串分成块。
该字符串类似于(您可能希望看到它):
Hi {{company_name}}\r\n\r\nA new order has been created via your website, from a customer with the following details:\r\n\r\n<table border=\"1\">\r\n <tr>\r\n <td>Full Name</td>\r\n <td>{{ customer.full_name }}</td>\r\n </tr>\r\n <tr>\r\n <td>Email</td>\r\n <td>{{ customer.email }}</td>\r\n </tr>\r\n <tr>\r\n <td>Mobile</td>\r\n <td>{{ customer.mobile }}</td>\r\n </tr>\r\n <tr>\r\n <td>Landline</td>\r\n <td>{{ customer.landline }}</td>\r\n </tr>\r\n <tr>\r\n <td>Notes</td>\r\n <td>{{ notes }}</td>\r\n </tr>\r\n</table>\r\n\r\nThese products or services were ordered:\r\n\r\n{% if appointment_items != empty %}\r\nBooking Details\r\n<table border=\"1\">\r\n <tr>\r\n <td>Type</td>\r\n <td>Description</td>\r\n <td>Price</td>\r\n</tr>\r\n{% for line_item in appointment_items %}\r\n<tr>\r\n <td>{{ line_item.category }}</td>\r\n <td>{{ line_item.short_description}}</td>\r\n <td>{{ line_item.price | display_price }}</td>\r\n</tr>\r\n{% endfor %}\r\n</table>\r\n{% endif %}\r\n\r\n{% if enquiry_items != empty %}\r\nEnquiry Details\r\n<table border=\"1\">\r\n <tr>\r\n <td>Type</td>\r\n <td>Description</td>\r\n <td>Price</td>\r\n</tr>\r\n{% for line_item in enquiry_items %}\r\n<tr>\r\n <td>{{ line_item.category }}</td>\r\n <td>{{ line_item.short_description}}</td>\r\n <td>{{ line_item.price | display_price }}</td>\r\n</tr>\r\n{% endfor %}\r\n</table>\r\n{% endif %}\r\n\r\n{% if has_appointment %}\r\n\r\nOne or more of the order items requires an engineer appointment. This appointment has been allocated to an engineer and given a provisional appointment time.\r\n\r\n<table border=\"1\">\r\n <tr>\r\n <td>Start</td>\r\n <td>{{ appointment.start | date_with_time_zone: \"%A %d %b %Y at %H:%M %P\" }}</td>\r\n </tr>\r\n <tr>\r\n <td>Finish</td>\r\n <td>{{ appointment.finish | date_with_time_zone: \"%A %d %b %Y at %H:%M %P\" }}</td>\r\n </tr>\r\n <tr>\r\n <td>Duration</td>\r\n <td>{{ appointment.duration }}</td>\r\n </tr>\r\n <tr>\r\n <td>Engineer Name</td>\r\n <td>{{ appointment.engineer }}</td>\r\n </tr>\r\n</table>\r\n\r\nThe address of the appointment is:\r\n\r\n<table border=\"1\">\r\n <tr>\r\n <td>Address Line 1</td>\r\n <td>{{ appointment.address.line_one }}</td>\r\n </tr>\r\n <tr>\r\n <td>Address Line 2</td>\r\n <td>{{ appointment.address.line_two }}</td>\r\n </tr>\r\n <tr>\r\n <td>Town/City</td>\r\n <td>{{ appointment.address.town_city }}</td>\r\n </tr>\r\n <tr>\r\n <td>County</td>\r\n <td>{{ appointment.address.county }}</td>\r\n </tr>\r\n <tr>\r\n <td>Postcode</td>\r\n <td>{{ appointment.address.postcode }}</td>\r\n </tr>\r\n</table>\{% endif %}
所以这个字符串是存储在数据库中的电子邮件,然后使用液体标签进行解析。
液体是一种诱人的语言。它具有变量的概念,例如:{{ var_name }}
和逻辑,例如 if 语句,例如:{% if var_name == true %} //do something {% endif %}
我需要能够获得最长的字符串,即 100% 将在这封电子邮件中发送出去。我的最终目标是编写一个测试来检查电子邮件正文中最长的字符串。
我想出了这个:
regex = /{{.*?}}|\\\n|\\\r|({%\s?if).*(endif\s?%})/
mail_template_content_part = @mail_template.content.split(regex).reject(&:empty?).map(&:strip).max_by(&:length)
正则表达式应该在液体 vars{{ my_var }}
新行上拆分,并在 an 的开头{% if
和所有内容上进行拆分endif %}
我喜欢这个max_by
方法:D
现在每个选项都单独工作。但是,当它们全部放在一起时,它们似乎不起作用。我不确定发生了什么,我怀疑拆分正在拆分,因此正则表达式的 if/end 部分没有运行。
所以我的问题是
正则表达式有什么问题?
拆分如何与正则表达式一起使用?
我会以正确的方式解决这个问题吗?
谢谢,
迪基