Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

using regular expression Problem  RSS feed

 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"\\w\\s*"-it finds first position Tobe a word then followed by a space



output:


start string eend
0 0
1 1
2 2
3 3
4 4
5 5


i think output should be
start string end
0 a 2
2 1 4
4 4
5 5

why it isn't finding a followed by space as it matches (\\w\\s)*?
and 1 followed by space
then what's the use ? in the regular expression..


if i remove ? ,it finds "a 1 2"
 
Gari Jain
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cannot understand the question?



What is that...a loop?
 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
these types of question come in OCPJP.
that's why i posted it
 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please somebody help
i had posted the question about 3 days ago
i haven't got a correct answer

 
Sumit Khurana
Ranch Hand
Posts: 68
Chrome Java MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

* is a greedy quantifier and *? is reluctant quantifier.
greedy quanitifier first go through the source and than come back and find the things.so,it knows all the source...
but *? operator is unwilling to do anything it prints the things in single go....it always do the easier things....*? means zero or more and it does not know about the things which will come after the current word....so,easier thing for it is to find zero.....
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!