Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

312
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda