473,804 Members | 3,081 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

recording positions of multiple substrings that match a regexp

Is there a way to get the position of multiple substrings that match a
regexp without using closures? match() returns the substrings
themselves, not the positions, and search() seems to only return the
first position. Here's what seems to work (under Shanti Rao's jsdb.exe
shell) but I get a bit nervous about using closures...

function bind_cache(x) { return function (subs,ofs)
{ x.push([ofs,subs.length]); return ""; }}
x=[];
f=bind_cache(x) ;
s='The quick brown fox jumped over the lazy dogs';
junk=s.replace(/\w*o\w*/g, f);

for (i = 0; i < x.length; ++i) writeln(s.slice (x[i][0],x[i][0]+x[i]
[1]));

output (all the words with an 'o'):
brown
fox
over
dogs

Apr 11 '07 #1
1 1763
On Apr 11, 6:50 am, "Jason S" <jmsa...@gmail. comwrote:
Is there a way to get the position of multiple substrings that match a
regexp without using closures? match() returns the substrings
themselves, not the positions, and search() seems to only return the
first position. Here's what seems to work (under Shanti Rao's jsdb.exe
shell) but I get a bit nervous about using closures...

function bind_cache(x) { return function (subs,ofs)
{ x.push([ofs,subs.length]); return ""; }}
x=[];
f=bind_cache(x) ;
s='The quick brown fox jumped over the lazy dogs';
junk=s.replace(/\w*o\w*/g, f);

for (i = 0; i < x.length; ++i) writeln(s.slice (x[i][0],x[i][0]+x[i]
[1]));
var
m,
x = [],
re = /\w*o\w*/ig
;
while ( m = re.exec( s ) ) {
x.push( [m.index, m[0]] );
}
alert( x.join( "\n" ) );

would be more attuned to the underlying facilities.

--
../rh

Apr 11 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
1367
by: borges2003xx | last post by:
hi everyone. a problem: two binary strings, a="0101" b="000011110100"; i search a function f(a,b) that gives 1 if a is "contained" in b with any sub strings interposed. in this example a in contained cause 000<01>111<01>00 but also 0<0>001111<101>00" but also <0>0001111<101>00 but also 000<0>1111<01>0<0> etc.... any idea? Thanx in advance.
4
4677
by: spam | last post by:
Is there a well-known algorithm for replacing many substrings in a string? For example, I'd like to take the string "abc def ghi jkl mno pqr" and replace, say, every instance of "abc", "ghi", and "mno" with another value. Of course the brute-force approach is straight forward. Just iterate over the full string N times (once for "abc", "ghi", and "mno"), find all instances, and replace them using the normal std::string member...
9
1421
by: Steven Bethard | last post by:
I've got a list of word substrings (the "tokens") which I need to align to a string of text (the "sentence"). The sentence is basically the concatenation of the token list, with spaces sometimes inserted beetween tokens. I need to determine the start and end offsets of each token in the sentence. For example:: py> tokens = py> text = '''\ .... She's gonna write .... a book?'''
32
14715
by: Licheng Fang | last post by:
Basically, the problem is this: 'do' Python's NFA regexp engine trys only the first option, and happily rests on that. There's another example: 'oneself' The Python regular expression engine doesn't exaust all the
6
2728
by: EXI-Andrews, Jack | last post by:
the '*' and '+' don't seem to be greedy.. they will consume less in order to match a string: ('aa', 'ab') this is the sort of behaviour i'd expect from '(a+?)(ab)' a+ should greedily consume a's at the expense of the string not matching
9
1363
by: shellon | last post by:
Hi all: I want to match a string that not ended with the string "welcome", how do write the RegExp? Appreciate your help!
5
8786
by: mikko.n | last post by:
I have recently been experimenting with GNU C library regular expression functions and noticed a problem with pattern matching. It seems to recognize only the first match but ignoring the rest of them. An example: mikko.c: ----- #include <stdio.h> #include <regex.h>
5
2250
by: czechboy | last post by:
Hi, what is the fastest way to search for all occurences of a string in a very long string (from several KB to MB). What I want to get is an array of numbers with positions of each occurence. thanks
3
1885
by: Happy Face | last post by:
Hi, All, I encountered this strange problem while using function preg_match. The following is the php code. when I set the line: $text = str_repeat('*', 12500); preg_match will return 0 for the hard coded regular expression. but if I set $text to 12499, it will return 1 for a match. $text = str_repeat('*', 12499);
0
9705
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9575
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10320
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10308
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7609
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6846
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5513
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3806
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2981
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.