aspose file tools*
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
Author

pattern Match without using Pattren or RegulaExpression

Osgar ali
Greenhorn

Joined: Jan 24, 2009
Posts: 22
Hi,
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

Thanks
Oskar
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

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 ;-)

Marco
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
wood burning stoves
 
subject: pattern Match without using Pattren or RegulaExpression