Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
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  
  Regex help (50th time already, sorry)  (Read 2007 times)
0 Members and 1 Guest are viewing this topic.
Offline K.I.L.E.R

Senior Member




Java games rock!


« Posted 2005-12-06 19:55:27 »

1  
2  
3  
4  
5  
6  
      VERTEX("^" + ".*?" + "v" +
            "(\\s-{0}?-{0,1}?\\d{1,}?){3,4}?" +
//            "\\s(-{0,1}?\\d+?)" +
//            "\\s(-{0,1}?\\d+?)" +
//            "\\s{0,}?"+"(-{0,1}?\\d+?){0,1}?" +
           ".*?" + "$"),

Is supposed to find:
v x y z w

Where 'w' is optional.
Initially I got it working however I was trying to simplify it greatly by doing this:

"(\\s-{0}?-{0,1}?\\d{1,}?){3,4}?"

Here is the problem.
I break up the original string into:

(\\s-{0}?-{0,1}?\\d{1,}?) + {3,4}?

1st half is supposed to look for something like this:
" -133"

The second part of the string is supposed to find that pattern a minimum of 3 times and a max of 4.
However the output is all wrong.
Why?

Sample output:
1  
2  
3  
4  
5  
6  
7  
8  
1.0, 0.0, 0.0, 0.0, 
-1.0, 0.0, 0.0, 0.0,
1.0, 0.0, 0.0, 0.0,
-1.0, 0.0, 0.0, 0.0,
1.0, 0.0, 0.0, 0.0,
-1.0, 0.0, 0.0, 0.0,
1.0, 0.0, 0.0, 0.0,
-1.0, 0.0, 0.0, 0.0,


How it should look:
1  
2  
3  
4  
5  
6  
7  
8  
1 1 1
1 1 -1
1 -1 1
1 -1 -1
-1 1 1
-1 1 -1
-1 -1 1
-1 -1 -1


Everytime I need regex it tears my heart out.
It's the one thing that really cracks me.

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

Jeff:
Unemployed. Wink
Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #1 - Posted 2005-12-06 20:30:55 »

Is supposed to find:
v x y z w

Where 'w' is optional.

"v x y z w?"

that's it, no?

really. That's why regexp's are great - they're really easy for handling lots of literal text

malloc will be first against the wall when the revolution comes...
Offline K.I.L.E.R

Senior Member




Java games rock!


« Reply #2 - Posted 2005-12-07 06:31:13 »

Unfortunately not great for handling dynamic amounts of text.

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

Jeff:
Unemployed. Wink
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline swpalmer

JGO Coder




Where's the Kaboom?


« Reply #3 - Posted 2005-12-07 06:50:27 »

Is supposed to find:
v x y z w

Where 'w' is optional.

"v x y z w?"

that's it, no?

really. That's why regexp's are great - they're really easy for handling lots of literal text

I understand where he is trying to go from the regex examples.. it seems that the "v" is literal, but x,y,z, and w are numbers in the real data.  (Thus the \\d in the regex for "digit")  Right?

Offline K.I.L.E.R

Senior Member




Java games rock!


« Reply #4 - Posted 2005-12-07 08:29:08 »

Yes.

THey can be negative numbers too.

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

Jeff:
Unemployed. Wink
Offline cylab

JGO Ninja


Medals: 38



« Reply #5 - Posted 2005-12-07 09:57:38 »

There are several constructs, that look suspicious in your regex  Tongue
1  
(\\s-{0}?-{0,1}?\\d{1,}?) + {3,4}?

The last '?' seems to make no sence (at least to me). I think you wanted to use reluctant matches to avoid getting the greatest possible match, but I don't see the application here. Also '-{0}?' seems a bit odd...

Could you post an excerpt of the data you like to parse?

Mathias - I Know What [you] Did Last Summer!
Offline noblemaster

JGO Ninja


Medals: 20
Projects: 10


Age of Conquest makes your day!


« Reply #6 - Posted 2005-12-07 10:05:14 »

Regex! Yep, it's cool but it's (1) hard to write and (2) hard for read. Why not make it easy on
yourself and others? You have to show me first some people that really understand it to
convince me it is great.

How about something easier:

1  
2  
3  
4  
5  
6  
7  
8  
String line = ...
StringTokenizer st = new StringTokenizer(line, " ");
int x = Integer.parseInt(st.next());
int y = Integer.parseInt(st.next());
int z = Integer.parseInt(st.next());
if (st.hasNext()) {
   w = ...
}


I would say that's easier to read and understand?

Offline K.I.L.E.R

Senior Member




Java games rock!


« Reply #7 - Posted 2005-12-07 10:08:03 »

Somtimes the data can be 4 dimensional.
x, y, z, w.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
v 1 1 1
v 1 1 -1
v 1 -1 1
v 1 -1 -1
v -1 1 1
v -1 1 -1
v -1 -1 1
v -1 -1 -1
f 1 3 4 2
f 5 7 8 6
f 1 5 6 2
f 3 7 8 4
f 1 5 7 3
f 2 6 8 4



There are several constructs, that look suspicious in your regex  Tongue
1  
(\\s-{0}?-{0,1}?\\d{1,}?) + {3,4}?

The last '?' seems to make no sence (at least to me). I think you wanted to use reluctant matches to avoid getting the greatest possible match, but I don't see the application here. Also '-{0}?' seems a bit odd...

Could you post an excerpt of the data you like to parse?

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

Jeff:
Unemployed. Wink
Offline cylab

JGO Ninja


Medals: 38



« Reply #8 - Posted 2005-12-07 10:45:56 »

Hmm:
1  
(\\s-?[^\\s]+){3,4}


But I have to say kingaschi might be right, since you must have code to process the regex anyway... Wink

Mathias - I Know What [you] Did Last Summer!
Offline K.I.L.E.R

Senior Member




Java games rock!


« Reply #9 - Posted 2005-12-07 13:58:49 »

My code processes it nicely and I understand the basics of how regex works:
"^" + ".*?" + "v\\s(\\d+?)\\s(\\d+?)\\s(\\d+?)\\s(\\d+?)" + ".*?" + "$"

However when it comes down to simplifying the above string to cater for a number of variables then it gets difficult.

cylab:
(\\s-?[^\\s]+){3,4} = doesn't work, gives me the same result as I already have.

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

Jeff:
Unemployed. Wink
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline cylab

JGO Ninja


Medals: 38



« Reply #10 - Posted 2005-12-07 14:54:39 »

Do you mind posting a complete example with the correponding java code.

I have not really thought about the whole context, so I am afraid the problem lies in referencing the match brackets. If you use a single bracket with {m,n}, you won't (to my knowledge) be able to reference the single matches, afaik only the last match will be returned... So I think you are stuck with your working "expanded" version of the regex.

Edit:
s/match bracket/capturing group/g Wink so I am essentially saying the same as pepijnve below  Roll Eyes

Mathias - I Know What [you] Did Last Summer!
Offline pepijnve

Junior Member




Java games rock!


« Reply #11 - Posted 2005-12-07 15:24:11 »

I think your simplified version doesn't work as expected because the second version defines less capturing groups than the first one.
The original regex you posted
1  
"\\s(-{0,1}?\\d+?)\\s(-{0,1}?\\d+?)\\s(-{0,1}?\\d+?)\\s{0,}?(-{0,1}?\\d+?){0,1}?"

defines four capturing groups, one for x, y, z and w respectively

The simplified one
1  
"(\\s-{0}?-{0,1}?\\d{1,}?){3,4}?"

defines only one capturing group that is matched 3 or 4 times. The contents of this capturing group will be the last succesful match.

Also I'm not sure why you are using all those {m,n} notations and reluctant quantifiers. Personally I would write it as
1  
2  
3  
4  
5  
6  
7  
Pattern p = Pattern.compile(
    "v"
    + "\\s(-?\\d*(?:,\\d*)?)"
    + "\\s(-?\\d*(?:,\\d*)?)"
    + "\\s(-?\\d*(?:,\\d*)?)"
    + "(?:\\s(-?\\d*(?:,\\d*)?))?"
);

which matches stuff of the form "v x y z w" where x, y, z and w can be any number and w is optional.
Offline K.I.L.E.R

Senior Member




Java games rock!


« Reply #12 - Posted 2005-12-07 18:04:54 »

So there is absoultely no way to simplify the case?

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

Jeff:
Unemployed. Wink
Offline cylab

JGO Ninja


Medals: 38



« Reply #13 - Posted 2005-12-07 18:30:11 »

I am afraid, no. Just out of curiousity, why are you so obsessed with regexes in this context, kingaschi proposal of using a StringTokenizer seems a valid solution...

Mathias - I Know What [you] Did Last Summer!
Offline jbanes

JGO Coder


Projects: 1


"Java Games? Incredible! Mr. Incredible, that is!"


« Reply #14 - Posted 2005-12-07 19:33:04 »

Somtimes the data can be 4 dimensional.

<inigo-montoya>You keep using that term. I do not it means what you think it means.</inigo-montoya>

I think you may be overgeneralizing the problem. Correct me if I'm wrong, but all you want to do is split each line into each "word", right? Then you need to test if you have three or four columns on that line. So something like this should do the trick:

1  
2  
3  
4  
5  
String line = in.readLine();
String[] values = line.split("\\ "); //A regex that selects spaces and splits on them

if(values.length == 4) //Do something
else //Do something else


I assume at some point you will want to convert some of the Strings to integers, so you'll need to remember to use Integer.parseInt(values).

Does that answer your question?

Java Game Console Project
Last Journal Entry: 12/17/04
Offline cborders

Junior Member





« Reply #15 - Posted 2005-12-07 20:04:25 »

Let me start off my saying that I am partial to StringTokenizers!  BUT, if you read a line and pass that to a tokenizer then getting the first token will tell you whether the data will have 3 or 4 parts to it.  Unless the sample you posted isnot representative!  For example:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
StringTokenizer tokenizer = new StringTokenizer(line);
String temp = tokenizer.nextToken();
if(temp.equals("v"))
{
    // 3D Stuff
}
else if(temp.equals("f"))
{
    // 4D Stuff
}
Offline K.I.L.E.R

Senior Member




Java games rock!


« Reply #16 - Posted 2005-12-08 14:43:49 »

I was hoping regex would be more powerful.
I don't like string tokenizers due to the fact that regex is more powerful and much more natural to process IMO.
I know how to use regex normaly however I wanted to further simplify my reg ex command to take into effect multiple variables.
Now that I know that it isn't possible I guess it's time to lobby Sun over added regex functionality. Smiley

Thanks guys.

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

Jeff:
Unemployed. Wink
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 746
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #17 - Posted 2005-12-08 15:31:21 »

Regex might be more powerful, but it's a waste of resources in this case.

Your OBJ loader (as that's what you're parsing) will be mH^H^H^Huch faster when you roll your own plain simple algorithm.

It matters, when loading large model files on the fly.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
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.

Riven (20 views)
2014-07-29 18:09:19

Riven (13 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (31 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (42 views)
2014-07-24 01:59:36

Riven (42 views)
2014-07-23 21:16:32

Riven (28 views)
2014-07-23 21:07:15

Riven (29 views)
2014-07-23 20:56:16

ctomni231 (60 views)
2014-07-18 06:55:21
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!