Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

299
Vistas
Match url path and ip address from string

How to match two parts of the string?

Example string:

/some/page_path 255.122.212.211

The first part I want to match is a valid path, a simple check if starts with / and no white spaces. (and maybe special signs?)

The second part is just checking the IP address if has a pattern of (0-9) numbers with dots.

Example of bad string:

/some/page _path 2525.122.212 yada yada

Regex I have for IP:

^(?:[0-9]{1,3}\.){3}[0-9]{1,3}
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

You can use

/\A(?:\/[^\/\s]+)+\s+(?:[0-9]{1,3}\.){3}[0-9]{1,3}\z/

See the Rubular demo. Details:

  • \A - start of string
  • (?:\/[^\/\s]+)+ - one or more occurrences of / and then one or more chars other than / and whitespace
  • \s+ - one or more whitespace chars
  • (?:[0-9]{1,3}\.){3}[0-9]{1,3} - three occurrences of one to three digits followed with a . char, and then one to three digits
  • \z - end of string.
over 4 years ago · Santiago Trujillo Denunciar

0

I have assumed that the last part of the string must be a valid IPv4 address.

require "ipaddress"
def str_parts(str)
  path, ip_str = str.split(/ +/,2)
  raise "'#{path}' is an invalid path" unless path_valid?(path)
  raise "'#{ip_str}' is an invalid ip address" unless ip_valid?(ip_str)
  [path, ip_str]
end
def path_valid?(path) 
  path.match?(/\A(?:\/[^\/\s]+)+\z/)
end
def ip_valid?(ip_str) 
  IPAddress.valid?(ip_str)
end

The regular expression I have used in path_valid? is taken from the first part of the expression given by @Wiktor. A more definitive operating system-specific check could of course be used. See the doc for the ipaddress gem. Note that I have used String#split's optional second argument.

str_parts("/some/page_path 255.122.212.211")
  #=> ["/some/page_path", "255.122.212.211"]
str_parts("/some/page _path 2525.122.212 yada Yoda")
  #=> RuntimeError ('_path 2525.122.212 yada Yoda' is an invalid ip address) 
str_parts("/some/page_path 255.122.256.211")
  #=> RuntimeError ('255.122.256.211' is an invalid ip address)
str_parts("/some//page_path 255.122.212.211")
  #=> RuntimeError ('/some//page_path' is an invalid path)

An alternative to using the ipaddress gem would be to write:

def ip_valid?(ip_str) 
  (IPAddr.new(ip_str) rescue nil)&.ipv4?
end

See IPAddr::new and IPAddr#ipv4?. & is Ruby's Safe Navigation Operator.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda