Win a copy of Classic Computer Science Problems in Swift this week in the iOS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Regular Expression with Scanner  RSS feed

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to parse a string that contains a Name=Value as:

blah=value;junk=avalue; User=one:two:three:name; morejunk=value

I need to grab the User=one:two:three:name; part of the string (User= is the fixed part of the string followed by 1 or more fields separated by colons), then take the value after the last colon, before the semicolon (name in this case).

What would be the Pattern to give in order to find the string I am interested in?

Is the best approach something like:


My experience with Scanner has been limited to a fixed delimiter, such as colons. This particular problem seems to fit Scanner's ability to deal with regular expressions / patterns.

Any suggestions are appreciated.
 
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might be trying to do too much in one regex. It's probably easier to grab the entire "User=one:two:three:name; ", then do some additional processing to find the part after the last colon.

But, to do it in one regex... how about

The part you're looking for should end up in group 1, the first set of parentheses. You can retrieve this with:

[ March 05, 2008: Message edited by: Mike Simmons ]
 
Rancher
Posts: 274
C++ Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Rolf Johansson:
I need to parse a string that contains a Name=Value as:

blah=value;junk=avalue; User=one:two:three:name; morejunk=value

I need to grab the User=one:two:three:name; part of the string (User= is the fixed part of the string followed by 1 or more fields separated by colons), then take the value after the last colon, before the semicolon (name in this case).

What would be the Pattern to give in order to find the string I am interested in?



Wonder if this will work for you?



Please let me know.

- Anand
 
Rolf Johansson
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies. I've tried both regular expressions, but neither locate the desired string. I've tried as:



and:

 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both the patterns work correctly - the problem is with the surrounding code, which I just wrote quickly without testing.

Anand's code is probably a more precise statement of what you want. But it depends whether you want to put any restrictions on what can be in a value. I used "\\w" for word characters, which means numbers or letters. Anand used "[^;]" for "anything but a ;" which is much more permissive. Neither of us know what sorts of values are really possible here, so that's up to you.

There is one problem with Anand's pattern: what if User has only one value with no ":"? Anand's pattern requires at least one ":" before the final value. We can modify it to make this optional:
 
Anand Hariharan
Rancher
Posts: 274
C++ Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Mike Simmons:
(...)
There is one problem with Anand's pattern: what if User has only one value with no ":"? Anand's pattern requires at least one ":" before the final value.



This was OP's requirement:

Originally posted by Rolf Johansson:

User= is the fixed part of the string followed by 1 or more fields separated by colons



Appreciate your testing and correcting OP's code.

- Anand
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But what does one field separated by a colon look like? I don't know for sure, but I'm guessing this was just a bit of sloppiness in the specification, and it seems likely that User=one; is a possible input. Either way, it's something Rolf should think about.
 
Rolf Johansson
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Specifications in original posting can justifiably be called sloppy

There will be 2 though 6 values separated by colons following User=
These values will always be comprised of digits and characters, so \\w will work.

I appreciate all the help!
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!