Re: Finding a pattern without regexp() - contd.
by "Srinivasan Ramakrishnan" <srinivar(at)md3.vsnl.net.in>
|
Date: |
Sat, 29 Apr 2000 22:37:50 +0530 |
To: |
"Cyanide _7" <leo7278(at)hotmail.com>, <hwg-languages(at)hwg.org> |
References: |
|
|
todo: View
Thread,
Original
|
|
Cyanide_7 , that was cool. I don't mind the split, I'll probably be leaving
the wild cards for later.
JS has such a great deal of abstraction that it's impossible to manipulate
strings without using atleast the "length" property and charAt() function.
All the other string functions are derivable from these two.
I'm looking for an algorithm (as the term implies it must be language
independent) to simulate regexp(). JS will be one of the implentation
platforms, so I guess simulation of regexp() will be the only thing
necessary, since functions like indexOf() already exist.
On other platforms if I wanted to simulate indexOf() & substring() in an
algorithm which does not assume any string handling functions, how do I go
about it ?
My idea is to use an array of char, and by various comparison operations
derive the higher level string functions. At this moment the entire problem
is only an idea in my mind. But I think it helps me keep my programming
brain stimulated.
Whether there are any practical outcomes to all this is yet to be seen, but
then when I'm having fun what do I care?
Cheers,
-Srini
----- Original Message -----
From: Cyanide _7 <leo7278(at)hotmail.com>
To: <srinivar(at)md3.vsnl.net.in>
Sent: Saturday, April 29, 2000 6:20 PM
Subject: Re: Finding a pattern without regexp() - contd.
| sirini,
| i think i've got the wild card part down. right now i have the '*' able
to
| represent ANY string of chars ANY length. even across words. this however,
| requires the split method, please tell me that split is at our disposal
| too!?!? in any case, i must say i'm really having fun with this! here's
the
| latest:
|
| <script>
| function indexOf(seg,str,start){
| var index = -1, i = start;
| while(index == -1 && i < str.length-seg.length)
| if(str.substring(i,i+seg.length) == seg)
| index = i;
| else
| i++;
| return index;
| }
|
| function findPat(seg,str){
| var term = " .,;!?";
| var index = -1, i = 0;
| segs = (" "+seg).split('*');
| parts = new Array(seg.length);
|
| while(index == -1 && parts[0] != -1){
| parts[0] = indexOf(segs[0],str,i);
| abort = (parts[0]==-1);
| j=1;
| while(j < segs.length && !abort){
| parts[j] = indexOf(segs[j],str,parts[j-1]+1);
| abort = (parts[j]==-1);
| j++;
| }
| if(!abort && indexOf(str.charAt(i+seg.length+1),term,0)!=-1)
| index = parts[0]+1;
| else
| i = parts[0]+1;
| }
| return index;
| }
| alert(findPat("Sat*day","Today is Saturday. Have a nice day!"));
| </script>
|
| although, i have no idea how you plan on using a data source in
JavaScript.
| please tell me this is server-side! other wise, good luck! - Cyanide_7
|
| >Hi,
| >
| >Since cyanide_7 wanted some info on the pattern to be matched here's the
| >info. I agree that a pattern matching equivalent o regexp() once
developed
| >will be useful.
| >
| >
| >I'm basically looking for a word, so the pattern would be a space,
followed
| >by the word entered by the user, usally 5-6 characters in length, follwed
| >by
| >another space, or a sentence terminator like a (.) or (?) or (!)
| >
| >Finally I will be extending this to handle wild cards also. And as you
can
| >see this is the part where regexp() comes in!
| >
| >Since I'm also be porting this to other platforms, which may or may not
| >have
| >indexOf() functions, I can't use them. Another version of this may be
using
| >a file as the data source, so I'm looking at something versatile enough
to
| >also handle a data stream.
| >
| >-Srini
| >
| >P.S: I come up with the vaguest problems don't I?
| >
| >
| >
| >
| >
| >----- Original Message -----
| >From: Srinivasan Ramakrishnan <srinivar(at)md3.vsnl.net.in>
| >To: <hwg-languages(at)hwg.org>
| >Sent: Saturday, April 29, 2000 10:20 AM
| >Subject: Finding a pattern without regexp()
| >
| >
| >| Hi,
| >|
| >| I need to find a pattern in a string using JS. Because of browser
| >| compatability I can't use regexp(). Do any of you know what has to be
| >done?
| >| Any code in C, Pascal or Java is ok as well.
| >|
| >| I have a vague idea about a pattern matching technique using stacks,
but
| >I'm
| >| not very clear on that. Any pointers will be appreciated.
| >|
| >| Cheers,
| >| -Srini
| >|
| >|
| >
|
| ________________________________________________________________________
| Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com
|
|
HWG: hwg-languages mailing list archives,
maintained by Webmasters @ IWA