The moose likes Java in General and the fly likes pattern Match without using Pattren or RegulaExpression Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "pattern Match without using Pattren or RegulaExpression" Watch "pattern Match without using Pattren or RegulaExpression" New topic

pattern Match without using Pattren or RegulaExpression

Osgar ali

Joined: Jan 24, 2009
Posts: 22
I am trying to write a java code to do a string pattern search. I don't want to use any Pattern or Regular Expression. Can someone give me some input how I can do that.

Here is an example:

String is: aaBaBaaaBaaa

if characters aBa is found in the above sting print B, if aaa is found print A.

Example: aaBaBaaaBaaa will out put BAA.

aaaBaBaaaaBBaBaBa will out put ABAB

Marco Ehrentreich
best scout

Joined: Mar 07, 2007
Posts: 1280

Hi Osgar,

perhaps one of the best ways to search a string for a specific pattern without using regular expression is the Knuth-Morris-Pratt algorithm. It's to complicated to explain here in a few lines but I'm sure you'll find lots of information about it in the internet. Good luck ;-)

Rob Spoor

Joined: Oct 27, 2005
Posts: 19651

Loop through the string and use String.regionMatches. If there is a match, don't just jump to the next character but go forward match.length(), where match is either aBa or aaa.

Altenatively, you could use a combination of indexOf calls:

You would have to test to see which of these two algorithms is faster, but I think it also depends on the number of occurrances.

How To Ask Questions How To Answer Questions
I agree. Here's the link: http://aspose.com/file-tools
subject: pattern Match without using Pattren or RegulaExpression
Similar Threads
print in JSP method
Problem with loop funcation related with arraylist's .size()(Resolved)
how to print in capital of each frist word
Help with recursion
A question about String operation