• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 10
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23951
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

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 Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
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

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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Appreciate your quick response Jeff Verdegan and pardon my ignorance.

Thanks
Jasheer
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".
 
Time is the best teacher, but unfortunately, it kills all of its students - Robin Williams. tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic