• 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
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

java regex : capturing groups that are optional

 
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author & internet detective
Posts: 41582
881
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.



 
Marshal
Posts: 77559
372
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving as too difficult for “beginning”.
 
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 41582
881
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Destroy anything that stands in your way. Except this tiny ad:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic