• Post Reply Bookmark Topic Watch Topic
  • New Topic

simple syntax question  RSS feed

 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, I should say I've put arrays on hold & gone to strings. I looked back at the two MOOCS I've used & saw that arrays are barely introduced before strings are brought into the picture to be used in arrays, so I decided to go on to strings so that I can go back to the MOOCs. My textbook is less than useless in helping me grasp arrays.

I got a string program to work, but that's partly because I found a place to help me do one thing I wanted to do: make sure that a string contained at least two digits.



What I want to know is what the two occurrences of .* mean. I think that the asterisks mean the whole string, but I have no idea what the periods mean. I found //d{2} in my book, but it didn't work without the .*, which is why I went in search of a solution. But I need to know what it means.

Thank you very much.
 
Junilu Lacar
Sheriff
Posts: 11164
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This might help you: https://docs.oracle.com/javase/tutorial/essential/regex/

and this https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html
 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd already been on one of those pages trying to figure it out, & on the other, I found plenty of backslashes (too tired; just wrote "blacksplashes") & even something that seemed to indicate I was right about the asterisk (that it pretty much means "the rest of it.") But I still can't figure out the periods. I just double checked, & they aren't spots on my dirty screen.

 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a regular expression (or regex for short), a dot means "match any character". But it means exactly one character.

If you want "zero or more" of something, you use a *. that means "I want zero or more of <whatever is just in front of the *>".

So when you have ".*", you are saying "zero or more of absolutely anything".

Without the two dot-stars, you would be looking for exactly two digits - nothing before it, and nothing after it. So a string like "abc123" would NOT match. the \\d{2} would mean the string has EXACTLY TWO DIGITS - no more, and no less.

Since you probably want to allow "abc123def", you need to allow for characters both before and after the two digits. But they must be optional, since you probably want to allow "12def".

Now...your regex may or may not be right. Do you want to allow "ABC1DEF2GHI"? as it is written now, this isn't allowed. Your regex requires the two digits be TOGETHER. That might not be what you want.
 
Ron McLeod
Saloon Keeper
Posts: 1600
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is mentioned in the Junilu's second link

    .   Any character (may or may not match line terminators)
 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred--thank you. I'm going to file your answer somewhere, because I don't understand it perfectly unless I have something right in front of me to which I am applying it, but your explanation is much clearer than most Java explanations I've been finding. Most things I find explain things I don't know in terms I don't know.

But you're right: I need to accept 2 or more digits even when they aren't together. How can I do that?

Thank you.
 
Carey Brown
Bartender
Posts: 2999
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't verified this but you might try

Translation: replace all characters that are not digits with a zero length string.
 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey--is that something I would want to do just to verify that there are at least 2 digits? Why would I want to replace them?
 
Carey Brown
Bartender
Posts: 2999
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are only replacing them in a copy of the string, not the string that you are passing in. Then the only thing you do with this copy is check the length.
I only suggest this because I can't think of a way to use regex to count digits that are interspersed with other non-digit characters.
 
Carey Brown
Bartender
Posts: 2999
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.S.
when you use (^) as the first character in a character set, it means NOT. So "[^0-9]" means all characters that are NOT digits.
 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh...oh...oh!!! That actually makes sense!!! I understand completely! Do you know how few times I've said that about Java in the last 3 weeks???

Thank you!
 
Carey Brown
Bartender
Posts: 2999
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karen Guffey wrote:Oh...oh...oh!!! That actually makes sense!!! I understand completely! Do you know how few times I've said that about Java in the last 3 weeks???

Thank you!
You're welcome.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regular expressions are not easy things to use at all. With very simple things they are usable, but they can get very complicated very quickly. They certainly aren't easy things for humans to read. To complicate things even more different languages use different 'flavours' of regular expression, so an expression that works in Java might not work if you're using the grep tool (a tool that lets you search text, available in Linux).

When I do use them I generally use a regular expression tester that lets you type in some text, type in a regular expression, and it will tell you which parts of the text match the regular expression.

A quick goolge search for 'Regular Expression tester Java' found the following site (others are available, and may or may not be better).

http://www.regexplanet.com/advanced/java/index.html
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:I haven't verified this but you might try

Translation: replace all characters that are not digits with a zero length string.


Karen Guffey wrote:Oh...oh...oh!!! That actually makes sense!!! I understand completely! Do you know how few times I've said that about Java in the last 3 weeks???


First of all, as others mentioned, with the OP only learning about Strings and Arrays, bringing in the Regular Expression engine as a tool is really not a good idea. Regexes are an order of magnitude more complex than strings and arrays !!!

Second, the replaceAll() solution is different from the matches() solution in the original post. The matches() is looking for at least two consecutive digits. The replaceAll() will yield true if there are at least two digits, regardless if any of them are consecutive or not.

Henry
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree that regexes can be vastly complicated. but they are worth learning. They're really their own little programming language, and analyzing what you need for the regex is just like analyzing what you need for a program.

So, Karen...think about what you need in your String. You are VERY close...

What you need is

zero or more characters, followed by
a digit, followed by
zero or more characters, followed by
a digit, followed by
zero or more characters

If you still want, try changing what you have to match the above. Then, as always, test the HECK out of it. pass in all alphabetics. all digits. strings of various lengths with one digit at the beginning, middle and end. With two digits in various places. pass in a zero length string. etc.

Regexes are hard. I'm not 100% sure what I spec'ed out above is 100% correct for all cases, but I think it's pretty close.
 
Carey Brown
Bartender
Posts: 2999
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, if you wanted to by-pass regex's all together, you'd need a count variable initialized to zero and then a loop to go through all the characters one-by-one, looking to see if they are a digit, and if they are, increment the counter.
 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am SO glad you guys said that regexes are hard, because "regular expression" sounds so innocuous, but all the descriptions make them sound important but simple. The only instructions I ever came across were "use a regular expression," & the definition was never helpful, because there were no clear examples. Well, not at my level, at any rate.

And I just now figured out that the group of characters I asked about must be a regular expression. "Regular"? Who named it???

I'm about to dive in, so I'll try out your suggestions when I've waited through all the other stuff I have to do. Serious question: how would you diagnose a person who studied Java on her own for a while, decided to take a class just for fun & to progress farther & faster, &, about 6 weeks into the course, started hating it & decided she'd never touch Java again after the class is over? As a teacher, I think it's a pretty sad state of affairs for a student to begin hating something she'd previously loved. I'm just hoping that, once this class is over, I feel motivated to pick up where I left off on the MOOCs I was doing. They were much better than my textbook.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karen Guffey wrote:Serious question: how would you diagnose a person who studied Java on her own for a while, decided to take a class just for fun & to progress farther & faster, &, about 6 weeks into the course, started hating it & decided she'd never touch Java again after the class is over?

I would diagnose you as "normal".

Programming is hard. Seriously. Everyone uses computers and thinks they are fun. Just like (almost) everyone drives a car. But have you ever considered trying to design a car? Try taking a mechanical engineering class for fun.

But I think it's worth it. There are many hurdles to overcome, but once you get past a few, it's like the clouds parting...it can be absolutely fantastic.


I think "regular" expression comes from more of a well defined, standard way, rather than commonplace or simple.

They can be simple. have you ever done a "dir *.txt" type command? that is a form of a regex.
 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the encouragement, Fred. My diagnosis, as a natural language professor, is that you need to get students to practice & master material before throwing more at them. I was looking forward to Strings, because I understand them fundamentally, but the chapter presents all the methods of the String class & talks about how characters work within the string, & then the very first exercise is pretty much "Here's the problem: now figure out which of the 10 methods we just introduced need to be used." The MOOCs present a BIT (not binary digit but tiny amount) of new material & then give a problem that require the student to the use that BIT of material.

Anyway, I hope the clouds part before the course ends!
 
Junilu Lacar
Sheriff
Posts: 11164
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:Try taking a mechanical engineering class for fun.

Oh no you didn't just make a dig on us mechanical engineers...

Hey, thermodynamics, fluid mechanics, kinematics, hydraulics, heating and cooling, robotics, ... ME is actually where all the fun is! We are the Jacks of all Trades of engineers
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
fred rosenberger wrote:Try taking a mechanical engineering class for fun.

Oh no you didn't just make a dig on us mechanical engineers...

My freshman year I was ME. And no offense was intended. It would have worked just as well with "design a PC/take a EE class" or any other engineering discipline. My father was an EE professor his entire professional career, my grandfather a chemical engineer. Nothing but love and respect for engineers here.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karen Guffey wrote:My diagnosis, as a natural language professor, is that you need to get students to practice & master material before throwing more at them. I was looking forward to Strings, because I understand them fundamentally, but the chapter presents all the methods of the String class & talks about how characters work within the string, & then the very first exercise is pretty much "Here's the problem: now figure out which of the 10 methods we just introduced need to be used." The MOOCs present a BIT (not binary digit but tiny amount) of new material & then give a problem that require the student to the use that BIT of material.

What many folk around here say, and say often, is that before you write a single line of code, you should write out in English (or whatever natural language you choose) exactly what you want your program to do. Step by step.

The, refine those steps, making it clearer and simpler. Then refine them again. Eventually, you want to have instructions you could hand a 10 year old child that they could follow without needed to ask you questions.

Only when you have that done should you start thinking about java terms/methods/classes. For something like this (check to make sure a string has at least two digits), I'd start with:

1) look at each character, and count how many are digits.

it's a good start, but a little vague. refinement 1:

1) look at each character in the string, one by one
2) for each one, if it is a digit, add 1 to a counter
3) when I'm done, if my counter is greater or equal to 2, print out "i have 2 or more digits"

Now it's a little clearer, but still some work needs to be done. refinement 2:

1) initialize some counter to 0
2) look at each character in the string, one at a time
3) if the current character is one of 0-9, add one to the counter's current value
4) when I'm done, if my counter is greater or equal to 2, print out "i have 2 or more digits"

ok...now we're getting somewhere. #1 is pretty straight-forward. I bet I could code that. And that's where I'd start. my first go at the code would be



(I'm assuming you're doing this in it's own method, not in main)

I would then compile and run that, and make sure I get out what i'd expect. If it works, I may add in the next piece..my next iteration might be this:



I'd compile and test that. I'd then fix any typos or errors, and compile again, until THAT works. I'd have to make a change to the spot that calls this method, but I could at first hard-code in a string. At this point, I don't care where the string comes from, I'm trying to test this method, so I'd pass in literal strings so I don't have to worry about user input or whatever.

And then keep going, adding a teeny, tiny piece at a time, compiling and testing OFTEN.

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