• Post Reply Bookmark Topic Watch Topic
  • New Topic

java regex : capturing groups that are optional  RSS feed

 
Lucky J Verma
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all
I need some help in java regex. I have a problem in capturing the last group which is optional. I have 3 strings I want to capture and 3rd on is optional.
Different input strings and input format is:
TS:This is system code[SYSCODE-123]
TS: This is system code[SYSTEM-123]
TS:This is system code.
TS12: This is system code


I need to extract three different string where 3rd group in input string is optional and I want tmy group to be returned 'null' in that case if possible.
three components :
TS
This is system code
SYSCODE (optional , I want to extract contents inside square brackets if present).
I am reformatting these strings in my UI and need to know if 3rd group is present or not.Somehow I am not able to make it work.
This is regex i am trying to modify to make it work.
Regex: (TS-.+):(.*)(\\[(.+)\\]);



Thnks.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37507
552
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code does approximately what you want. And it's completely unreadable (which shows proponents of avoiding regular expressions why they are right.) The gist is to break each piece up. And when searching for "any" character, it looks for any character until the next brace. This code doesn't print out null, but it is close enough that you can figure it out from here without reg exps.



 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving as too difficult for “beginning”.
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:I would use a non-capturing group for this.

You seem to be using capturing groups! Am I missing something?
This code does approximately what you want. And it's completely unreadable

It is readable to those who understand regular expressions ! I can't read Russian but that is not unreasonable since I have not studied it.
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What should the output be if there is the leading '[' of the "[something]" term but not the "]" ?

My approach is similar to Jeanne's but does provide 'null' if the "[something]" term is not present.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37507
552
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richard Tookey wrote:
Jeanne Boyarsky wrote:I would use a non-capturing group for this.

You seem to be using capturing groups! Am I missing something?

I removed that sentence. When I started writing the code, I was using a non-capturing group. THen I saw it wasn't needed and never went back to what I wrote.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!