Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

forming java pattern to find out occurance like repeated charactors in string

 
jasheer kandath
Greenhorn
Posts: 10
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 21506
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
jasheer kandath
Greenhorn
Posts: 10
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21506
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Appreciate your quick response Jeff Verdegan and pardon my ignorance.

Thanks
Jasheer
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
jasheer kandath
Greenhorn
Posts: 10
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic