requiring balanced parens in a regexp?

Discussion in 'Javascript' started by Peter Michaux, Nov 10, 2006.

  1. 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
    Peter Michaux, Nov 10, 2006
    #1
    1. Advertising

  2. Peter Michaux

    shimmyshack Guest

    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.
    shimmyshack, Nov 10, 2006
    #2
    1. Advertising

  3. 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
    Peter Michaux, Nov 10, 2006
    #3
  4. In message <>, Thu,
    9 Nov 2006 22:20:01, Peter Michaux <> writes
    >
    >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.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
    Dr J R Stockton, Nov 10, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stephen Ferg

    How to avoid "f.close" (no parens) bug?

    Stephen Ferg, Feb 11, 2004, in forum: Python
    Replies:
    12
    Views:
    483
    Nick Craig-Wood
    Feb 13, 2004
  2. Batista, Facundo

    RE: How to avoid "f.close" (no parens) bug?

    Batista, Facundo, Feb 11, 2004, in forum: Python
    Replies:
    2
    Views:
    240
    Neil Hodgson
    Feb 11, 2004
  3. Joao Silva
    Replies:
    16
    Views:
    337
    7stud --
    Aug 21, 2009
  4. ivo welch

    matching balanced parens

    ivo welch, Dec 26, 2003, in forum: Perl Misc
    Replies:
    3
    Views:
    95
    Ben Morrow
    Dec 27, 2003
  5. Bart Lateur
    Replies:
    2
    Views:
    73
    Bart Lateur
    Sep 3, 2007
Loading...

Share This Page