Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (517)
Games in Android Showcase (123)
games submitted by our members
Games in WIP (578)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Any good Regexp tutorials?  (Read 1487 times)
0 Members and 1 Guest are viewing this topic.
Offline Breakfast

Senior Duke




for great justice!


« Posted 2005-04-28 13:55:10 »

Not game related- hence it being in this forum- but every time I read anything by any kind of programmer who knows anything about anything they always rave about the life-transforming power or Regexps and how they will improve every aspect of your life if you even think about them. I've been thinking about them and what I've thought is that I have no idea how to use them well.

Can anyone suggest any good resources for learning about them? Ideally stuff that takes you from basic to advanced without a)  assuming you're a moron or b) jumping from incredibly simple to awesomely complex without a word in between. I realise that's a heck of a lot to ask...
Offline swpalmer

JGO Coder


Exp: 12 years


Where's the Kaboom?


« Reply #1 - Posted 2005-04-28 15:41:30 »

Start with the javadocs  http://java.sun.com/j2se/1.5.0/docs/api/java/util/regex/Pattern.html

Then google from there.
e.g.
http://www.regular-expressions.info/tutorial.html

Keep in mind that often the regular expression you see printed have a backslash character and so when you enter then in java source code as a String you must escape the backslash by using a double backslash.. this can make the regex in the code hard to read when compared to the 'real' expression.

e.g. \d  matches a digit, but as a Java string it must be "\\d"

Offline Breakfast

Senior Duke




for great justice!


« Reply #2 - Posted 2005-04-29 07:51:53 »

And if I'm escaping the backslash for a literal I need "\\\\" ?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline swpalmer

JGO Coder


Exp: 12 years


Where's the Kaboom?


« Reply #3 - Posted 2005-04-30 02:30:11 »

Yes, that's covered in the second link above.

Offline William Denniss

JGO Coder


Projects: 2


Fire at will


« Reply #4 - Posted 2005-05-01 00:35:37 »

I wrote a small Java regex guide which I refer to a lot when doing java regex (especially since I seem to do it fairly sporadically ).  It does NOT teach you much about regular expressions, but does help one take that knowledge and apply it to java (so useful if you already know about regex, or a reading non-java tutorial about it).

http://tanksoftware.com/tutes/article-regexprimer.pdf

It is designed to be used with the Pattern javadoc's which give you all the info about character classes etc. that you need.

Will.

Offline K.I.L.E.R

Senior Duke




Java games rock!


« Reply #5 - Posted 2005-05-01 06:45:58 »

How's this for use of regex?
Can anyone improve this further?
I'm still not great at using regex.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
private void convertParamToObj(String word)
      {
            Scanner scanner = new Scanner(word);
           
            if(Pattern.matches("WORLD = \\d*, \\d*, \\d*, \\d*;",  word))
            {
                  String type = "\\d+";
                  int row = Integer.parseInt(scanner.findInLine(type));
                  int cols = Integer.parseInt(scanner.findInLine(type));
                  int width = Integer.parseInt(scanner.findInLine(type));
                  int height = Integer.parseInt(scanner.findInLine(type));
                 
                  world = new World(row, cols, width, height);
            }
           
            scanner.close();
      }

Vorax:
Is there a name for a "redneck" programmer?

Jeff:
Unemployed. Wink
Offline cfmdobbie

Senior Duke


Medals: 1


Who, me?


« Reply #6 - Posted 2005-05-01 15:07:30 »

Quote
How's this for use of regex?
Can anyone improve this further?


I recommend using the Matcher class instead:

1  
2  
3  
4  
5  
6  
7  
8  
9  
Matcher m = Pattern.compile("WORLD = (\\d+), (\\d+), (\\d+), (\\d+);").matcher(word);
if(m.matches())
{
      int row = Integer.parseInt(m.group(1));
      int cols = Integer.parseInt(m.group(2));
      int width = Integer.parseInt(m.group(3));
      int height = Integer.parseInt(m.group(4));
      world = new World(row, cols, width, height);
}


Hellomynameis Charlie Dobbie.
Offline swpalmer

JGO Coder


Exp: 12 years


Where's the Kaboom?


« Reply #7 - Posted 2005-05-01 20:18:34 »

I'm with cfmdobbie, but note also that you used an inconsistent pattern,

For the row you used the asterisk - meaning 0 or more, for the field matching you used the plus - meaning 1 or more.

If your input can have an empty field e.g.  1, , 3, 4  then the asterisk * is required in both places (cfmdobbie's code would need a tweak).  You can also be a bit more resilient in terms of dealing with white-space.

"(\\d*)\\s*,\\s*(\\d*)\\s*,\\s*(\\d*)\\s*,\\s*(\\d*)"

That would allow spaces or tabs before or after the commas, and the numbers could be omitted entirely if you wanted the concept of an empty field.  (Just remember that if you allow empty fields the group could be an empty string and therefore it won't parse as an Integer.)

Offline K.I.L.E.R

Senior Duke




Java games rock!


« Reply #8 - Posted 2005-05-01 20:55:13 »

Thanks guys. Is the regex fast enough to use in a game during runtime?

Currently I'm using it to load maps from plain text files.

Vorax:
Is there a name for a "redneck" programmer?

Jeff:
Unemployed. Wink
Offline cfmdobbie

Senior Duke


Medals: 1


Who, me?


« Reply #9 - Posted 2005-05-01 22:20:40 »

swpalmer: I rather assumed that that was an oversight - those values all looked rather important to me! Wink

Quote
Thanks guys. Is the regex fast enough to use in a game during runtime?

Currently I'm using it to load maps from plain text files.


Cripes, yeah.  Sitting next to a bunch of disk I/O you really won't notice it at all. Roll Eyes

However, like any behind-the-scenes String-processing code, I'd avoid using it in a render-loop.

Hellomynameis Charlie Dobbie.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline swpalmer

JGO Coder


Exp: 12 years


Where's the Kaboom?


« Reply #10 - Posted 2005-05-01 22:54:46 »

Being a general solution it will be a bit slower than if you optimized something to do exactly what you want and only that.  (e.g. indexOf() on a string is faster than finding a substring using a regex)

You can compile the pattern once and save it to avoid that cost throughout the game... but really you should just profile the part that is using it to see if it really matters or not.

Offline Deadcow

Senior Newbie




Back from beyond ... Moo!


« Reply #11 - Posted 2005-05-04 09:18:19 »

Quote
Thanks guys. Is the regex fast enough to use in a game during runtime?

Currently I'm using it to load maps from plain text files.


Pattern's static factory method "compiles" the regular expression in a state machine. Once compiled, a regular expression is quite fast, taking character as input event and switching from state to state. So you can use a regex in a loop very efficiently, but try to compile it outside of the loop.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

DarkCart (15 views)
2014-10-31 21:44:48

DarkCart (19 views)
2014-10-31 21:43:57

TehJavaDev (40 views)
2014-10-27 03:28:38

TehJavaDev (30 views)
2014-10-27 03:27:51

DarkCart (44 views)
2014-10-26 19:37:11

Luminem (27 views)
2014-10-26 10:17:50

Luminem (30 views)
2014-10-26 10:14:04

theagentd (36 views)
2014-10-25 15:46:29

Longarmx (64 views)
2014-10-17 03:59:02

Norakomi (62 views)
2014-10-16 15:22:06
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!