| Author |
forming java pattern to find out occurance like repeated charactors in string
|
jasheer kandath
Greenhorn
Joined: Feb 27, 2010
Posts: 10
|
|
Hi all,
Which would be the best way to form a java pattern to find out the presence of repeated characters or numbers (Example: 111, eee, aaa, 444) and sequential letters or numbers (abc, bcd , xyz 012, 123, etc) inside a jva string.
Thanks\
jazz
|
SCJP 1.6
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
jasheer kandath wrote:Hi all,
Which would be the best way to form a java pattern to find out the presence of repeated characters or numbers (Example: 111, eee, aaa, 444) and sequential letters or numbers (abc, bcd , xyz 012, 123, etc) inside a jva string.
Thanks\
jazz
By "pattern", I am assuming you mean regular expressions?
Regex is very good at finding repeated characters -- take a look at the qualifiers that search of "n or more" (for your case, I am assuming n equals 3). Unfortunately, Regex is not good at finding sequential characters (sorry).
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
jasheer kandath
Greenhorn
Joined: Feb 27, 2010
Posts: 10
|
|
Thanks for the quick response henry.
Do you know of any other alternatives to find the sequential characters using java. What i think of now is just looping in the string chars and find out with the use of some falgs and ASCI values.
thanks,
Jasheer
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 5901
|
|
Henry Wong wrote:Unfortunately, Regex is not good at finding sequential characters (sorry).
What do you mean here? You mean the same character repeated multiple times, like the OP is asking?
will do that.
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 5901
|
|
jasheer kandath wrote:
Do you know of any other alternatives to find the sequential characters using java.
Sure. You write about 5-10 lines of code that iterates over the String, checking each character.
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
Jeff Verdegan wrote:
Henry Wong wrote:Unfortunately, Regex is not good at finding sequential characters (sorry).
What do you mean here? You mean the same character repeated multiple times, like the OP is asking?
will do that.
No. I was referring to the second use case.
Henry
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 5901
|
|
Henry Wong wrote:
Jeff Verdegan wrote:
Henry Wong wrote:Unfortunately, Regex is not good at finding sequential characters (sorry).
What do you mean here? You mean the same character repeated multiple times, like the OP is asking?
will do that.
No. I was referring to the second use case.
Henry
Oops. Totally missed that there even is a second use case.
Duh, Jeff. "Sequential."
|
 |
jasheer kandath
Greenhorn
Joined: Feb 27, 2010
Posts: 10
|
|
Hi,
The usage of "(.)\\1+" gives the result when the character is repeated more than once. How can I change so that it will detect for only more than 2 characters . For example it should detect only it for "aaa" and not "aa".
Thanks,
jazz
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 5901
|
|
jasheer kandath wrote:Hi,
The usage of "(.)\\1+" gives the result when the character is repeated more than once. How can I change so that it will detect for only more than 2 characters . For example it should detect only it for "aaa" and not "aa".
Thanks,
jazz
You should take a look at a regex tutorial
http://docs.oracle.com/javase/tutorial/essential/regex/
http://www.regular-expressions.info/tutorial.html
However, what you want would be
Meaning "any character, followed by 2 or more of that same character". (Note that this includes spaces, punctuation, everything. I'll leave it up to you to adjust which characters you want to catch.)
|
 |
jasheer kandath
Greenhorn
Joined: Feb 27, 2010
Posts: 10
|
|
Appreciate your quick response Jeff Verdegan and pardon my ignorance.
Thanks
Jasheer
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4761
|
|
jasheer kandath wrote:Appreciate your quick response Jeff Verdegan and pardon my ignorance.
That still doesn't solve your problem of searching for 'abc', which, as Henry said, regex, was not designed for. For that you may have to fall back on Jeff's string search suggestion.
Wisnton
|
Isn't it funny how there's always time and money enough to do it WRONG?
|
 |
jasheer kandath
Greenhorn
Joined: Feb 27, 2010
Posts: 10
|
|
yeah.. I am onto that one
I would have been much better if sun engineers would have included that in regx.patterns :P
thanks
Jasheer
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 5901
|
|
jasheer kandath wrote:
yeah.. I am onto that one
I would have been much better if sun engineers would have included that in regx.patterns :P
thanks
Jasheer
I don't know if that's in any regex implementation.
And "better" is a very subjective and context-dependent term. It would maybe be "more convenient for Jasheer Kandath at this particular moment", but that's not the same thing as "better".
|
 |
 |
|
|
subject: forming java pattern to find out occurance like repeated charactors in string
|
|
|