• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regular Expression - Set of Values  RSS feed

 
author
Bartender
Posts: 4093
21
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I get a huge headache whenever I try to use even basic Java regular expression matching (mostly because online examples never seem to be close to what I need), but I thought rather than doing things with lots of substring/indexof commands, I thought I'd give regex a shot.

Assume you have a string of the following format:
...Grid={t:200, b:300, r:1, o:32}...

Where "..." can be any non-structured text other than the format of the string you're matching. How would you use regular expressions to read the four integer values: 200, 300, 1, 32?

Again, I could use a lot of substring/indexof to do it, but is there a simple way to use regular expressions to get the values? Also, I know you can use the split command (on the commas, then on the :'s), but again this seems like a lot of objects created for a small string (the parsing will be executed millions of times).
[ January 07, 2008: Message edited by: Scott Selikoff ]
 
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you can guarantee the spaces it would be something like this:



Eric

[edit - switched \n to \d]
[ January 07, 2008: Message edited by: Eric Pascarello ]
 
Scott Selikoff
author
Bartender
Posts: 4093
21
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure I follow, basically I would have multiple lines such as:

...Grid={t:200, b:300, r:1, o:32}...
...Grid={t:3040, b:10, r:994, o:94}...
...Grid={t:592, b:5000, r:34, o:942}...
...Grid={t:489, b:400, r:599, o:342}...

And I want to quickly read the values in each set in into array such as short[4].
[ January 07, 2008: Message edited by: Scott Selikoff ]
 
Scott Selikoff
author
Bartender
Posts: 4093
21
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be precise my goals are:

1) to be able to find this exact pattern in a large string

2) to be able to quickly parse the integer values contained within the matching string
 
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never mind
[ January 07, 2008: Message edited by: Marc Peabody ]
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, typed it real quick \n is a line feed should be \\d



Eric
[ January 07, 2008: Message edited by: Eric Pascarello ]
 
Scott Selikoff
author
Bartender
Posts: 4093
21
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Eric,

I don't get the function of the replace command. I'm trying to find the values, not replace them. How would you use the code to read the values?
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String foo = yourString.replace();

would have foo = "###,###,###,###";

You could split it and have your array.

I am not the greatest at Java so not sure if there is another way of using match or something similiar to get the values into an array.

Eric
 
author & internet detective
Marshal
Posts: 37698
575
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To get results from Eric's regexp:


[edited to disabled smilies]
[ January 07, 2008: Message edited by: Jeanne Boyarsky ]
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37698
575
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eric got his reply in first, but mine still makes sense. Cool!
 
Scott Selikoff
author
Bartender
Posts: 4093
21
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lol, thanks everyone. One of the things I was missing was to call group() with different input arguments. The full running code that matches the pattern and outputs the info is:


[ January 07, 2008: Message edited by: Scott Selikoff ]
 
Scott Selikoff
author
Bartender
Posts: 4093
21
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW, the double escaping of "\" is annoying in java+regex!
 
Bartender
Posts: 18883
77
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Scott Selikoff:
BTW, the double escaping of "\" is annoying in java+regex!



This is, alas, because both Java and regexes (and Unix in general) use the same escape character.

At least Eclipse can take an escaped regex string and java-escape it. Small consolation, but better than none.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!