Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

311
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!