• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help List in groovy with regex

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Olá
eu preciso fazer uma validação em uma lista de strings groovy.

a validação é, dentro da lista só pode haver strings que terminam com ".com"

se algum elemento dentro da lista não terminar com ".com" ou, vier algum elemento vazio na lista, o resultado é false

se todos os elementos dentro da lista vier com final ".com" e não tiver algum elemento vazio na lista, o resultado é true

-------------------------------------------------
Hello
i need to do a validation on a list of groovy strings.

the validation is, inside the list there can only be strings that end with ".com"

if any element within the list does not end with ".com" or, there is an empty element in the list, the result is false

if all elements within the list end with ".com" and there are no empty elements in the list, the result is true




example of list false:

def scopes = ["CONT.com", "", "CA.com", "TOP.br", "GUYS.cear", ".read", "com"]

example of list true:

def scopes = ["CONT.com", ".com", "CA.com", "TOP.com", "GUYS.com", ".com"]




boolean result = scopes.findAll{it.endsWith(".READ")}
def checaRead = scopes.findAll{it.endsWith(".READ")}
def vejaRead = scopes.any {".READ"}
def validaReadWithRegex = scopes.findAll { it =~ /.READ/ }
boolean  validaReadWithRegexs = scopes.findAll { it ==~ /\.READ/ } //aqui ja pesquisa com o ponto e palavra pra validar

println(result)
println(checaRead)
println(vejaRead)
println(validaReadWithRegex)
println(validaReadWithRegexs)



----------------------------------------------------------
 
Ranch Foreman
Posts: 883
8
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Peter Palva,

Welcome to Code Ranch.
I don't know the best answer.   I was wondering what would happen if you tested one item in the list at a time using regex.  If there is a blank or no .com at the end then exit with false.  I don't know the performance difference over a single correct regex expression on a list.

I have a Mastering Regular Expressions book by O'reilly on my book shelf so I looked in the index for list.   I think there is information in it around page 310 that has to do with your question.

I wish I could be more help but I saw nobody answering your post.

Thanks,

Kevin
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Peter Paiva wrote:if all elements within the list end with ".com" and there are no empty elements in the list, the result is true


An empty element doesn't end with ".com", so maybe you could rewrite that as

if all elements within the list end with ".com", the result is true


Which would simplify your solution, perhaps?
 
kevin Abel
Ranch Foreman
Posts: 883
8
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul,

Good idea.  I didn't think of it like that.

Thanks,

Kevin
 
reply
    Bookmark Topic Watch Topic
  • New Topic