jQuery in Action, 3rd edition
The moose likes Java in General and the fly likes Is this possible with regex? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Is this possible with regex?" Watch "Is this possible with regex?" New topic

Is this possible with regex?

Ravi Shankarappa
Ranch Hand

Joined: Jan 09, 2010
Posts: 55
Following is a line of code in ALGOL 60

In Java I could write it as

Is there a way to do this programmatically using regex or something else? If so could you tell me how?

fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11952

Of course it can be done - but the answer is always "it depends". You need to define better what you need.

for example, it could be done in a java program quite simply. Assuming the String s holds

"FOR" K:=N-1 "STEP" -1 "UNTIL" 1 "DO"

you could write this:

but I'm guessing that won't actually solve your problem.

My point is that you have to decide what situations you will and won't handle. Only once you have ALL those conditions clearly thought out and defined should you consider writing a single line of java/regex/perl/whatever to solve your problem.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Darryl Burke

Joined: May 03, 2008
Posts: 5115

Also note that regex doesn't help in converting between uppercase and lowercase.

luck, db
There are no new questions, but there may be new answers.
Ravi Shankarappa
Ranch Hand

Joined: Jan 09, 2010
Posts: 55
I was looking for a more general solution via substitution. I only need to take care of 174 routines in ALGOL 60, so its not a big effort. I understand regex isn't much use in converting upper case to lower case, then again it is easily done via built in tolowercase functionality of String class.
There are sections of code that are not easily convertible via parser, anyway. Also I need to take into consideration the machine precision. So except for some obvious substitutions, I'll have to look at each line of code. Writing a parser might take a longer time than diving into converting by hand.

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46328
The reason converting by hand is faster than a parser is that you already have a parser written, and you can find it in the back of your head which is a smart place to keep it. It is by no means difficult to write a parser, eg with lex and yacc (JFlex and CUP are more modern counterparts) or ANTLR or similar, if the grammar is context-free. You will find lex, flex, JFlex etc, use regular expressions frequently to identify numbers, keywords, etc.
I agree. Here's the link: http://aspose.com/file-tools
subject: Is this possible with regex?
It's not a secret anymore!