Validate IP address or hostname with port & range check with RegExpValidator
-
QT 5.9.2 … Android
^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3})(:\d{1,5})$|^((([a-zA-Z]|[a-zA-Z0-9][a-zA-Z0-9-][a-zA-Z0-9]).)([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9-][A-Za-z0-9]))(:\d{1,5})*$
FoobarHost
FoobarHost:12345
Ip
Ip:12345Works with online reg tester https://regex101.com with /m multiline option but not with QT Qml … is there a way to validate ip and hostname
TextField {
id: serverIpText
height: 24
maximumLength: 255
cursorVisible: true
// DNS allows alphanumeric and hyphens... dots are in IP address... and : separates optional port number
//
validator: RegExpValidator {
regExp: /^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3})(:\d{1,5})$|^((([a-zA-Z]|[a-zA-Z0-9][a-zA-Z0-9-][a-zA-Z0-9]).)([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9-][A-Za-z0-9]))(:\d{1,5})*$/
}
} -
I know its an old topic but it was the first search result so im gonna post my solution
your regex is wrong and when you try to test a regex on regex101 dont forget to select your programming language.
here is a working regex which allows you to extract ip address or hostname
^(?<qfdn>^[a-zA-Z]+.+\\.{1}[a-zA-Z]{2,}$)|(?<ip>^(?:(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)\\.){3}(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d){1}$)$
this works on javascript so i think qml too