473,396 Members | 1,995 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

requiring balanced parens in a regexp?

Hi,

In the following string I would like to find the word that comes after
"test" as long as test is not inside parenthesis. In this example the
match would be "two".

"the (test one) test two"

I found some indication that Perl regexp can do this with some sort of
recursive regexp. Can JavaScript regular expressions ensure that all
parentheses to the right of "test" are closed before proclaiming a
match? If so how? If not must I walk through the string counting how
nested each character is?

Thank you,
Peter

Nov 10 '06 #1
3 1852
could you just use

\)[\w ]*[test ]([\w]+)[ \w^\)]*\(

note that this automatically searches what is not enclosed and grabs
the word after test, and if a repeated open bracket is found, then no
match occurs.

I mean i havent tested it, and i should be asleep, but thats where i
would start off. (this assumes that you dont have a string as you gave
above, but in fact have an arbitrarily long string with many such
matches possible. You must make sure you take care of the start and end
of the string if you are grabbing a substring of a larger string,
before you attempt matches.

Nov 10 '06 #2
Hi Shimmyshack,

shimmyshack wrote:
could you just use

\)[\w ]*[test ]([\w]+)[ \w^\)]*\(

note that this automatically searches what is not enclosed and grabs
the word after test, and if a repeated open bracket is found, then no
match occurs.

I mean i havent tested it, and i should be asleep, but thats where i
would start off. (this assumes that you dont have a string as you gave
above, but in fact have an arbitrarily long string with many such
matches possible. You must make sure you take care of the start and end
of the string if you are grabbing a substring of a larger string,
before you attempt matches.
Thanks for the suggestion. I think that if I allow nested parens then
this won't work. In the following "test two" looks like it is outside
some brackets.

((test one) test two (test three)) test four

I think that my whole idea has gone down the tubes. I need to write a
tokenizer and a real parser.

Thanks again,
Peter

Nov 10 '06 #3
In message <11*********************@f16g2000cwb.googlegroups. com>, Thu,
9 Nov 2006 22:20:01, Peter Michaux <pe**********@gmail.comwrites
>
Thanks for the suggestion. I think that if I allow nested parens then
this won't work. In the following "test two" looks like it is outside
some brackets.

((test one) test two (test three)) test four

I think that my whole idea has gone down the tubes. I need to write a
tokenizer and a real parser.
Consider looping on replacing ( any number of not ( or ) ) by
space until there was no change, then doing a simple search on the
residue. Undertested :-

St = "((test one) test two (test three)) test four"

function RP(S) { return S == (S=S.replace(/\([^()]*\)/g, " ")) ? S :
RP(S) }

Answer = RP(St).match(/\btest\s+(\S+)/)[1]

The g is optional.
The \b may need more thought, if "()test a" and other odd cases are
to be matched.

The code needs error-detection for the case where "test word" is not
found.
To be really clever, adapt it to work with matching () {} []
using only a single RegExp and .replace .

It's a good idea to read the newsgroup and its FAQ. See below.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Nov 10 '06 #4

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

Similar topics

10
by: Anand Pillai | last post by:
To search a word in a group of words, say a paragraph or a web page, would a string search or a regexp search be faster? The string search would of course be, if str.find(substr) != -1:...
5
by: Lukas Holcik | last post by:
Hi everyone! How can I simply search text for regexps (lets say <a href="(.*?)">(.*?)</a>) and save all URLs(1) and link contents(2) in a dictionary { name : URL}? In a single pass if it could....
0
by: Chris Croughton | last post by:
I'm trying to use the EXSLT regexp package from http://www.exslt.org/regexp/functions/match/index.html (specifically the match function) with the libxml xltproc (which supports EXSLT), but...
1
by: Koen | last post by:
Hi all, I created a little database to manage my e-books. The program will synchronize a table with the contents of a directory. Works great. Because I keep additional info (like keywords) to...
4
by: Jon Maz | last post by:
Hi All, I want to strip the accents off characters in a string so that, for example, the (Spanish) word "práctico" comes out as "practico" - but ignoring case, so that "PRÁCTICO" comes out as...
26
by: Matt Kruse | last post by:
Are there any current browsers that have Javascript support, but not RegExp support? For example, cell phone browsers, blackberrys, or other "minimal" browsers? I know that someone using Netscape...
13
by: Chris Bellini | last post by:
Greetings! I would like to use Server.Transfer to redirect users to a given page, while maintaining the state of form fields. This works fine on a single server. However, this will be...
6
by: runsun pan | last post by:
Hi I am wondering why I couldn't get what I want in the following 3 cases of re: (A) var p=/(+-?+):(+)/g p.exec("style='font-size:12'") -- // expected
5
by: David C. Ullrich | last post by:
There's no regex that detects balanced parentheses, or is there? That is, search('and so ((x+y)+z) = (x+(y+z))') should return '((x+y)+z)'. Not just a theoretical question, I'm cleaning up a...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.