If you’re just interested in UML syntax
If you want to express some conditions, restrictions or invariants in UML, you may use an UML constraint. The constraint is then expressed between curly braces { ... }
either in natural language or in OCL.
You could use an attribute constraint, just next to the attribue:
------------------------------------------
IpAddrV4
------------------------------------------
Address: String { matching regex '...' }
Or you could use a more general class constraint, writing your constraint in a comment-box connected to the relevant class with a dashed line:
{ inv: Address matching regex ... }
As far as I know, OCL specifications do not provide native support for regex. But some dialects (e.g. Eclipse) do, according to this SO answer.
If you want to get more from your model
What's the hidden problem in your modelling approach ?
Your question focuses on a possible implementation of the IPv4 address. You presume it's a string that needs to comply with a certain constraint, and your model leaks this assumption.
But are you really interested only in a specific representation of an IPv4 address as a string ? Or are you interested in what the address really is and what you can do with it ? And in the latter case, what are the behaviors of this class ?
I fear that with your current approach, you risk to end up with an UML model that just transcribes your current code into a graphical presentation. You then might not enjoy the full benefits that a real model could provide, such as abstraction, encapsulation, and separation of concerns.
What's an IPv4 address
Going back to the roots of RFC 791 (section 2.3) it appears that an IP address:
- is made of four octets, so 32 bits (and this 32 bit representation is used in the low-level functions) ;
- can be viewed as a network address followed by a local address;
- has an "address class" that governs the layout of the network and local address.
It appears in RFC 1180 (section 2) that when a user input provides an IP address, it should use the doted decimal numbering format.
The POSIX standard proposes some standard conversion between doted decimal string and internet address.
The UML class
I'd propose something like, to which you need to add the above-mentionned constraint on the string format:
----------------------------------------------
IpAddrV4
----------------------------------------------
-address: Integer //UInt32 in fact
----------------------------------------------
+IPAddrV4()
+IPAddrV4(Address: String)
+GetAddress() : String
+GetBinaryAddress(): Integer //UInt32 in fact
+GetAddressClass(): String //Char in reality
----------------------------------------------
Final word
Sorry if this might sound rude. But designing in 2019 an IPv4 application sounds weird and incomplete. The advantage with abstraction, is that it could allow you to design a more general address that would be able to cope as well with IPv6 formats and inner structure.