OCLinEcore and Regex to create valid IPv4 string

北城余情 提交于 2019-12-11 01:11:31

问题


I have an Ecore model, where I'm trying to add some OCL constraints.

My problem comes when I try to make a property (a string) that is in valid IPv4 form. I guess I should use a regular expression, but I have no idea at all about how to use regex on OCLinEcore.


回答1:


As of Eclipse Juno, you can use regular expressions in OCL. The function to call on it is matches(regex), just like in Java.

context Packet
inv ValidIPv4 : ip.matches('\b([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\b')

Source: http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.ocl.doc%2Fhelp%2FCompleteOCLTutorial.html

If you have a previous version of Eclipse, then I suggest adding a Java constraint on your Ecore element, then validate your element through the generated Java template code.




回答2:


As for the regex part:

\b([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\b

Matches 0.0.0.0 through 999.999.999.999 Use this fast and simple regex if you know the data does not contain invalid IP addresses. Each of the 4 numbers is stored into a capturing group, so you can access them for further processing.



来源:https://stackoverflow.com/questions/10940289/oclinecore-and-regex-to-create-valid-ipv4-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!