• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Question on Pattern Matching

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
holly all,

I'm studying for my scjp 1.5, and pattern matching comes out unfortunately.

I do have some questions :

import java.util.regex.*;



Is this true (My understanding)? : Find me groups of 1 of integer, and when you found one, print me out the index of the found integer based on the String and the group of 1 integer.

the output in compiler :



However, if i add a * at the back of \\d, which is :



the compiler prints out :


Why is this so? I thought what i'm telling the compiler is "Hey compiler, find me groups which start with a digit, while you're on it, take anymore integers behind the first one and form that into a group. Then move on the next token."

I really hope someone can give me some feedback. My exam is like less than 2 weeks away, and i'm really new on this chapter. My lecturer said that we weren't taught this before, but he skipped it.. I mean, wth. lol
 
Ranch Hand
Posts: 2108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to note that we are encouraged at Javaranch to use real sounding names. Not names of Singapore Idol winners, unless that is really your name.
 
Brandon Bay
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another question even I read the book, I still can't get it

heres what happened (Taken from Kathy/Berts' book) :

Let's see some of these formatting strings in action:




I understand that % is needed, and 2$ represents the second element(or index) in the expression? What about the > ? Really hope to get someone to clarify




im just so lost
 
Brandon Bay
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jesus Angeles:
Just to note that we are encouraged at Javaranch to use real sounding names. Not names of Singapore Idol winners, unless that is really your name.



I'm really sorry, my name's Taufik actually, but did not really want to reveal my full name, so substituted half of it..
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy "Taufik M." !

Thanks for your first contribution to this forum and...

Welcome to the Ranch!




Hope you'll enjoy.




Only one small issue: The Java Ranch follows a certain policy regarding user names.
The main reasons why and a link how to change yours you'll find here:
http://www.javaranch.com/name.jsp


So, could you please change your user name before your next posting?
It will not affect anything you've already posted here. Just your user name will update.


I'm posting this because I am one of the moderators of this forum.


Yours,
Bu.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Taufik M.:
...did not really want to reveal my full name.


That's okay, but please see the JavaRanch naming policy for how to handle that.
 
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are really concerned about revealing your real name, you can always put a fake name as long as it sounds like a real name.

I did some looking into your first question:

When you do Pattern.compile("\\d*"); it's saying find me a match with 0 or more digits. What you are really looking for is Pattern.compile("\\d+"); find me a match with 1 or more digits. If you replace * with a + that should do it.

As to why the * prints 01234456 is because when you have 0 or more digits, pretty much everything makes m.find() return true. At least that's what I think. So it goes by every charcter and return true:

a:true but empty group
b:true but empty group
3:true group:34
e:true but empty group
f:true but empty group

thus:0123445

as to the extra 6 in the end, it puzzles me a bit. Seems like there is a hidden character of some sort that is always attached to the end and always makes m.find() return true even though there is no group printed. When you use * or ?.
 
Jesus Angeles
Ranch Hand
Posts: 2108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Taufik M.:


I'm really sorry, my name's Taufik actually, but did not really want to reveal my full name, so substituted half of it..



I hope we did not scare you away. Just follow the name guidelines mentioned above. Someone already attempted to answer your question. I hope you stay with us in this friendly ranch.
 
Straws are for suckers. Now suck on this tiny ad!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic