Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Have Simple Code Snippet, Please Point Out My Error

 
Bd Howard
Greenhorn
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

As seen below, I am trying to figure out how split my sample string so that the "012" is not placed into the string array. After reading the Javadocs, I know that ^0-9 and \D are equivalent, and my output confirms this, but I thought those would return anything NOT a digit. So I tried the regex [a-z][A-Z] on splitChar which I thought would return only letters, but as you see below, that succeeded in returning the whole sample string, digits and all.

This leads me to believe either I have a fundamental misunderstanding of how this whole regex thing works, or I am missing something easy in my code.




And here are my results from the above code:


Length is: 5
012
This
is
the
string

Length is: 1
012

Length is: 1
012

Length is: 1
012 This is the string




Thank you for your help. :-)

BD
 
Bd Howard
Greenhorn
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was able to get the result I was looking for using:

String[] splitNum = name.split("[0-9]{3}\\s");

I was hoping for a way to just take a shortcut in stripping out those digits, but it now appears to me that it will be a multi-step process to do what I want to do. The above code was just to learn what was going on.

Oh well. :-)

BD
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic