• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Reg Ex question

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to parse a String using reg ex.

This is my code:



I want an array of all the strings between [ and ] (so {"dfdsfdf","dsfsdfs", "sdfsdfsdf", "sdf546456", "17/06/1966", ...}).

But I get:


Anyone who knows the answer?
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The split() method uses a regex to specify the delimiter -- not what you are looking for. So, if what you are looking for is neatly separated by delimters, then it will work.

I recommend that you uses the find() method, in a loop. And use regex groups to extract the items in the brackets.

Henry
 
Henry Wong
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But to answer why you are getting what you are getting... The delimiter is...



\\[* -- Zero or more occurances of the open square brace.
[^\\]] -- One character that is *not* a close brace.
\\] -- One character that is a close brace.

In most cases, the first part is zero characters. And the second and third part, uses one close brace, and one letter before it, as a delimiters. This is why most of your items are merely missing the close brace, and one letter before it.

Henry
 
Stijn Janssens
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henri,

I modified my code and now it works

 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The best you could do with split() is split on " " and get an array of all
your items but with the enclosing brackets as well. Use the Scanner object
to do it.
reply
    Bookmark Topic Watch Topic
  • New Topic