• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

split string and number

 
mark smith
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

i search a way to split a string in two area...
one for string and another for number...

example

aa34
come
aa 34

z342
come

z342

i tried with regex without success

any idea?

thanks
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mark smith wrote:
i search a way to split a string in two area...
one for string and another for number...

example

aa34
come
aa 34

z342
come

z342

i tried with regex without success

What did you try?

I see that in your first example, the result has a space between the letters and the number, but in your second example, the result does not.

For a regular expression to work, the first thing to do is to define clearly what is regular about it. Is your String always in the format of n-letters and m-numbers without spaces, gaps or white space? If you can define the pre-parse and post-parse formats clearly, we can help you towards a workable solution.
 
mark smith
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my error...

aa34
come
aa 34

zt42
come
zt 42

i don't know the number of letter and the number of digit...

the only thing i know it's that begin by a letter and end by a nubmer

i tried to find the first number and to split before this position... a regex seem a better solution
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mark smith wrote:
i don't know the number of letter and the number of digit...
the only thing i know it's that begin by a letter and end by a nubmer
i tried to find the first number and to split before this position... a regex seem a better solution


I'm no regex expert, but perhaps using look-ahead and look-behind for a non-digit word character next to a digit character is what you need. For e.g.,


i.e.,
 
Deepak Prakash
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pete stein wrote:
mark smith wrote:
i don't know the number of letter and the number of digit...
the only thing i know it's that begin by a letter and end by a nubmer
i tried to find the first number and to split before this position... a regex seem a better solution


I'm no regex expert, but perhaps using look-ahead and look-behind for a non-digit word character next to a digit character is what you need. For e.g.,


i.e.,


Cool..
 
Dovev Hefetz
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Apache Commons libarary has splitByCharacterType(), for anyone searching the forums.

http://commons.apache.org/lang/api-2.5/org/apache/commons/lang/StringUtils.html#splitByCharacterType%28java.lang.String%29

 
Campbell Ritchie
Sheriff
Pie
Posts: 49849
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Dovev Hefetz. That looks a useful method.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic