• Post Reply Bookmark Topic Watch Topic
  • New Topic

simple Regex  RSS feed

 
Bruno Sant Ana
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

My doubt is quite simple. Can someone explain how this expression [a-cA-C] works please?



when run this code appears: 0 1 4 5 6

Thank you
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bruno Sant Ana wrote:Hi,

My doubt is quite simple. Can someone explain how this expression [a-cA-C] works please?


Please SearchFirst.(⇐click)

http://docs.oracle.com/javase/tutorial/essential/regex/
http://www.regular-expressions.info/tutorial.html
 
Bruno Sant Ana
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thank you for the reply. I searched on some links and I looked at this link:

http://docs.oracle.com/javase/tutorial/essential/regex/char_classes.html

There is a piece of text in the link that seems to be very similar that one I posted before:
[a-zA-Z] a through z, or A through Z, inclusive (range)

So, [a-cA-C] means any letter between 'a' through 'c' and any letter between 'A' through 'C', right?

Another doubt related to regex, especifically this code:



I don't understand how this m.group works e why the output is 01234456. I find something here:
http://www.coderanch.com/t/531320/java/java/patterns-regex#2409330
http://www.coderanch.com/how-to/java/SCJP-FAQ#kb-regexp
but still don't understand.

Thanks
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bruno Sant Ana wrote:I don't understand how this m.group works e why the output is 01234456.

Neither do we unless you show us the arguments you supplied (which are, presumably, different from the ones you provided in your first post).
You must TellTheDetails (←click).

However, a general piece of advice:
Whenever you program, ALWAYS, ALWAYS, ALWAYS have a copy of the Java API documentation open on your desktop.
From there, it's but a few short mouse clicks to find the documentation for Matcher.group().

If, once you've read it, you still don't get it, come back and tell us what you don't understand - and be specific.

Winston
 
Bruno Sant Ana
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[a-cA-C] means any letter between 'a' through 'c' and any letter between 'A' through 'C', right?

The arguments passed to the program(last code I posted) are:
java Regex2 "\d*" ab34ef

I read the Java API but still don't understand.

Thank you again.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bruno Sant Ana wrote:[a-cA-C] means any letter between 'a' through 'c' and any letter between 'A' through 'C', right?

Right.

The arguments passed to the program(last code I posted) are:
java Regex2 "\d*" ab34ef

OK.

I read the Java API but still don't understand.

Yes, but what don't you understand?

The documentation is very clear:
"Returns the input subsequence matched by the previous match."
or to put it slightly simpler, and in your specific case:
"Returns the portion of the input that matched the previous call to find()."

If you're still having problems, try and explain them a bit more precisely.

Winston
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan has give two links to tutorials. Have you read those tutorials? I know the Oracle one is good.
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:http://www.regular-expressions.info/tutorial.html

This is an excellent introduction to Regular Expressions. The site as a whole is quite useful as a reference; I visit it often.
 
Bruno Sant Ana
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Ok, let me explain better my doubt. If I run the program using

java Regex2 "\d*" ab34ef

and modify the System.out inside the loop to

System.out.print(m.start());

It should return the positions where a numeric digit appears. In this case 2, right? Until here ok. If it's not 2 I'm sorry, I'm unable to run the program:
bruno@pc:~$ javac Regex2.java
bruno@pc:~$ java Regex2 "\d*" ab34ef
Exception in thread "main" java.lang.NoClassDefFoundError: Regex2
Caused by: java.lang.ClassNotFoundException: Regex2

now, if I put m.group like that:
System.out.print(m.start() + m.group());
the output is 01234456. I didn't understand why the output is 01234456 and how m.group works. Maybe for native speakers the documentation is clear and your explanation of it too, but for me they aren't. Why the method group make appears the indexes 012456 and why appears the number 34? Why is there 6 if I have just 5 indexes?

To be sincere, I don't understand very much of this code even after read the api, the Kathy's book and your explanation. Sorry, but it don't get clear for me, and it's not lack of effort.

Thanks
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bruno Sant Ana wrote:To be sincere, I don't understand very much of this code even after read the api, the Kathy's book and your explanation. Sorry, but it don't get clear for me, and it's not lack of effort.

Then we need to back up.

Do you understand what a Pattern is, and what it does?
Do you understand what a Matcher is, and what it does?

If you can't answer "yes" to both those questions, then you need to read the tutorials or re-read the relevant chapter in Kathy Reichs
before you try to decipher that program.

And I second Dennis' post; the link that Jeff gave you is excellent.

Winston
 
Bruno Sant Ana
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I got it and I'll explain in case another person has the same doubt. We can write this program changing it a little bit:



The output is:

pos [0] ==> str []
pos [1] ==> str []
pos [2] ==> str [34]
pos [4] ==> str []
pos [5] ==> str []
pos [6] ==> str []

Note that group() always return a String but it's an empty String when find() doesn't match any number. When find() match some number group() return the substring matched, in this case 34. As the \d* means zero or more occurrences, it shows index 6 that is the empty String after f(the last letter in the Matcher).

If I said something wrong you can correct me.

Bye.
 
Bruno Sant Ana
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the key is that phrase:

Because a match of zero length is possible, the find() method will check the index following the last character of input.
http://www.coderanch.com/how-to/java/SCJP-FAQ#kb-regexp
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bruno Sant Ana wrote:Because a match of zero length is possible, the find() method will check the index following the last character of input.

Well done. Isn't it much better when you work things out for yourself? Now you're never likely to forget.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!