Re: Finding a pattern without regexp() - contd.

by "Cyanide _7" <leo7278(at)hotmail.com>

 Date:  Sat, 29 Apr 2000 07:13:48 CDT
 To:  srinivar(at)md3.vsnl.net.in
 Cc:  hwg-languages(at)hwg.org
  todo: View Thread, Original
>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,

phwew! thought we were going to have to simulate [0-9] and such!

>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 (!)

sounds easy enough...but

>
>Finally I will be extending this to handle wild cards also. And as you can
>see this is the part where regexp() comes in!

which ones? some allow as many different chars, some only letters (i think), 
and some allow only one char. (not necessarily how RegExp works, but in 
other languages as well).

>Since I'm also be porting this to other platforms, which may or may not 
>have
>indexOf() functions, I can't use them.

function(S)?!? indexOf can be rewritten, though a bit less efficient i'm 
sure. what exactlly DO you have access to in the way of strings? substring 
and charAt ok? and if you need case insensitive, is toUpperCase ok?

>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.

this is what i've come up with so far. searches for and returns the index of 
the matching pattern, and -1 if not found. looks for preceding space and 
terminating characters:

<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;
  seg = " " + seg;
  while(index == -1 && i != -1){
    i = indexOf(seg,str,i);
    if(i!=-1 && indexOf(str.charAt(i+seg.length)+"",term,0)!=-1)
      index = i;
    else if(i!=-1)
      i++;
  }
  return index+1;
}
alert(findPat("day","Today is Saturday. Have a nice day!"));
</script>

still needs wildcard support etc... lemme know if i'm close, or if anyone 
has any ideas to make this more efficient. - Cyanide_7

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