Reputation: 771
I am trying to create a regex for IP
capture or hostname
, and ignore anything after #
DATA
192.168.0.41 #obs
SRVNET #obs
192.168.0.4 #obs
REGEX
^(([1]?[0-9]{1,2}|2([0-4][0-9]|5[0-5])).){3}([1]?[0-9]{1,2}|2([0-4][0-9]|5[0-5]|[a-z]))$
Upvotes: 0
Views: 42
Reputation: 163207
In the first part of the pattern from the comments you are matching [^#]+
which is a negated character class and will also match a space.
As you don't want to match spaces, you could add \s
to it to not match whitespace characters.
The whole match is wrapped in group 1, but as that is the only match you might make it non capturing (?:
Note that you have to escape the dot to match it literally and that [1]
is the same as just 1.
^(?:[^#\s]+)|^((1?[0-9]{1,2}|2([0-4][0-9]|5[0-5]))\.){3}(1?[0-9]{1,2}|2([0-4][0-9]|5[0-5]))$
^^ ^^ ^^
Upvotes: 1