This week's book giveaway is in the Artificial Intelligence and Machine Learning forum.
We're giving away four copies of Zero to AI - A non-technical, hype-free guide to prospering in the AI era and have Nicolò Valigi and Gianluca Mauro on-line!
See this thread for details.
Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Regular expression in Java

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am confused of what does  .* means when we put it on both sides of regular expression?
why did they add   .* on both sides while matching password ?


 
Marshal
Posts: 70625
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please always tell us where material comes from. Did whoever wrote that code not give an explanation? I can only presume it means any number of anything followed by a lower case letter followed by any number of anything. Go through the Java™ Tutorials, which give you a nice introduction to regular expressions. You will find that . means anything except line end and * means any number (including 0) of occurrences. So I think anything with a lower case letter in will match. I think the idea is to find at least one lower case letter (line 5), one upper case letter (line 11), etc. I don't know what lengthDifference means.
 
Saloon Keeper
Posts: 7386
66
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The same, only different. find() bails out as soon as it finds any match. Doesn't have to match the full string. (premature optimization?) I think it's slightly easier to read.

 
Ranch Foreman
Posts: 122
12
Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In regular expression, .* matches any string sequences (in a same line of course). When you have .* on both side, that means the string can start and end with anything before and after [a-z]. That is to say, if a string has [a-z] in it, no matter what other characters before and after it, the regular expression matches.

So:
".*[a-z].*" matches if: the string CONTAINS (at least one) lower case letter.
".*[A-Z].*" matches if: the string CONTAINS (at least one) upper case letter.
".*[0-9].*" matches if: the string CONTAINS (at least one) number.
"[a-zA-Z0-9]*" matches: all characters in the string are letters and numbers.
 
Carey Brown
Saloon Keeper
Posts: 7386
66
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
True, but by introducing .* into the regex you cause the matcher  to have to process the entire string just to find that out when, perhaps, the first character would have told you.
 
Frank Mi
Ranch Foreman
Posts: 122
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right.
 
Campbell Ritchie
Marshal
Posts: 70625
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you going to notice that sort of optimisation?
 
Md Zuanyeed Kamal
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Frank Mi wrote:
".*[a-z].*" matches if: the string CONTAINS (at least one) lower case letter.
".*[A-Z].*" matches if: the string CONTAINS (at least one) upper case letter.
".*[0-9].*" matches if: the string CONTAINS (at least one) number.
"[a-zA-Z0-9]*" matches: all characters in the string are letters and numbers.



Thank you so much for your explanation. However, if we have regular expression on only one side "[a-z].*" , or ".*[a-z]" , then what would it mean in general ? or how would you explain that ?
 
Md Zuanyeed Kamal
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:The same, only different. find() bails out as soon as it finds any match. Doesn't have to match the full string. (premature optimization?) I think it's slightly easier to read.



elegant approach, but length of code has been increased from 29 lines to 44 lines. However, I wanted to use only String class matches() method.
 
Frank Mi
Ranch Foreman
Posts: 122
12
Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Md Zuanyeed Kamal wrote:However, if we have regular expression on only one side "[a-z].*" , or ".*[a-z]" , then what would it mean in general ? or how would you explain that ?



"[a-z].*" : starts with a lower case letter (whatever after the first character doesn't matter)
".*[a-z]" : ends with a lower case letter (whatever before the last character doesn't matter)
".*[a-z].*" : contains a lower case letter (a string has a lower case letter in the middle, whatever before or after it doesn't matter)
 
Campbell Ritchie
Marshal
Posts: 70625
288
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Md Zuanyeed Kamal wrote:. . . length of code has been increased from 29 lines to 44 lines.

Much of that difference is spurious, caused by different indentation conventions and including the whole of the class.

. . . String class matches() method.

Only say “class method” if it is labelled static.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic