• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Text Parsing with Regex

 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I am trying to find a generic method to extract information from strings like:

"id:36 sub:001 dlvrd:001 submit date:0704270919 done date:0704270919 stat ELIVRD err:000"

I would like to be able to extract the values of particular items by defining a Regex style pattern. I am a bit of a regex newbie and I am not sure how to go about this. I had thought about using StringTokenizer but some of the value names have spaces in them and unfortunately I have no control over the format of these strings.

Can you use regex expressions to 'name' specific variable sections of the patterns so that they can be extracted by a Matcher?

Any help will be very welcome, thanks
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

The java.util.Scanner has much more capabilities than StringTokenizer. Have a look at that class, you can delimited with regex also, moreover it has methods to return specific types.

Cheers.
 
Andy Bowes
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Omer

Thanks for the reply.
I have worked it out using Regex and it works like a dream

Andy
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How did you handle the names with spaces in them? I have to guess the values cannot have spaces, or you'd never know where a value ends and a name begins.
 
Andy Bowes
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A Regular Expression can include spaces in the Pattern e.g.


[ May 02, 2007: Message edited by: Andy Bowes ]
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, cool, you made a pattern with all known keys and got them all with one match. I was thinking of something generic to get one key:value at a time which would survive insertion of a new key:value one day but wouldn't be near as self documenting.
 
I'm sure glad that he's gone. Now I can read this tiny ad in peace!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic