import Foundation
let pattern = #"\b((?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(?:(?<!\.)\b|\.)){4}"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = ##"""
#Ejemplos de codigo fuente del lenguaje de programacion Ruby
#Aritmetica basica
resultado='5 * (12 - 8) + -15'
puts resultado
puts 98 + (59872 / (13 * 8)) * -52
#Textos
puts "Hola, mundo!"
puts ""
puts "Adiós."
puts "Me gusta" + "el pastel de manzana."
puts "Mi IP es: 192.168.9.10"
puts "ragustin726@gmail.com"
puts "https://www.facebook.com"
puts "31/01/2021/"
277.893.21.2
#Variables
myString = '...puedes decir eso de nuevo...'
puts myString
name = 'Patricia Rosanna Jessica Mildred Oppenheimer'
ip= 10.0.0.1
correo='roberto_840@hotmail.com'
direccion='https://www.google.com'
fecha = '19/07/2001'
#Concatenaciones
puts 'Me llamo ' + name + '.'
puts 'Wow! "' + name + '" es un nombre realmente largo!'
composer1 = 'Mozart'
puts composer1 + ' fue "el amo", en su día.'
composer = 'Beethoven'
puts 'Pero yo prefiero a ' + composer1 + ', personalmente.'
#Conversiones
var1 = 2
var2 = '5'
puts var1.to_s + var2
var1 = 2
var2 = '5'
puts var1.to_s + var2
puts var1 + var2.to_i
#Condicionales
puts 1 > 2
puts 1 < 2
puts 5 >= 5
puts 5 <= 4
puts 1 == 1
puts 2 != 1
puts 'gato' < 'perro'
#Ramificaciones
puts 'Hola, y bienvenido a la clase de 7mo año.'
puts 'Me llamo Mrs. Gabbard. ¿Tú nombre es...?'
nombre = gets.chomp
if nombre == nombre.capitalize
puts 'Por favor, toma asiento ' + nombre + '.'
else
puts '¿' + nombre + '? Quieres decir ' + nombre.capitalize + ', ¿cierto?'
puts '¿No sabes escribir tu propio nombre?'
respuesta = gets.chomp
if respuesta.downcase == 'si'
puts '¡Hum! Bueno, ¡siéntese!'
else
puts '¡SALGA DEL SALON!'
end
end
#Bucles
comando = ''
while comando != 'adios'
puts comando
comando = gets.chomp
end
puts '¡Vuelve pronto!'
#Operadores logicos
edad=18
sexo="Mujer"
ocupacion="Estudiante"
if edad>=18 and sexo=="Mujer" and ocupacion="Estudiante"
puts "Mujer mayor de edad estudiante"
end
edad=18
if edad>=18 or edad<=25
puts "Tu edad esta entre los 18 y 25 años"
end
#Modulos y potencias
puts 5**2
puts 5**0.5
puts 7/3
puts 7%3
puts 365%7
#Lectura de archivos
content1 = File.read("imagen.jpg") # lee el archivo
content2 = File.read("contenido.png")
lines = content.split("\n") # divide el contenido en líneas
# recorre las líneas y las imprime
lines.each do |line|
puts line
end
"""##
let stringRange = NSRange(location: 0, length: testString.utf16.count)
let matches = regex.matches(in: testString, range: stringRange)
var result: [[String]] = []
for match in matches {
var groups: [String] = []
for rangeIndex in 1 ..< match.numberOfRanges {
let nsRange = match.range(at: rangeIndex)
guard !NSEqualRanges(nsRange, NSMakeRange(NSNotFound, 0)) else { continue }
let string = (testString as NSString).substring(with: nsRange)
groups.append(string)
}
if !groups.isEmpty {
result.append(groups)
}
}
print(result)
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Swift 5.2, please visit: https://developer.apple.com/documentation/foundation/nsregularexpression