Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4007818
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:40:41+00:00 2026-05-20T08:40:41+00:00

I’m trying to match the contents that belong between a certain ( and its

  • 0

I’m trying to match the contents that belong between a certain ( and its matching ) as found by vim when using the motion %.

More specifically, I’m looking for a regex that looks like this hypothetical /someKeyword (\{pair}\(.*\))\{pair}/, if there were such modifiers as \{pair} that when applied to two exactly two characters in a regex, makes the second one only match if it’s the matching bracket to the first one (%-wise).

The pattern I’m looking for should match the inner contents of the first bracket following someKeyword (n.b. the code that it should work on is always correctly bracketed), as in the examples:

For someKeyword ("aaa") the submatch will match "aaa".
Likewise someKeyword ("aaa)") will match "aaa)" and someKeyword(("double-nested stuff")) will match ("double-nested stuff")

But also in cases like:

(
  someKeyword("xyz"))

where it should match "xyz".

Is there any way to make use of vim’s matching bracket functionality in regexes? And if not, what other solution might work to accomplish this?

Edit 1: the matched contents may span several lines.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T08:40:41+00:00Added an answer on May 20, 2026 at 8:40 am

    This is not possible with vim regular expressions (as language that allows such nested constructs is not regular), but is possible with ‘regular’ expressions provided by perl (as well as by other languages I do not know enough to be sure) and perl can be used from inside vim. I don’t like vim-perl bindings (because it is very limited), but if you know all cases that should work, then you could use recursion feature of perl regular expressions (requires newer perl, I have 5.12*):

    perl VIM::Msg($+{"outer"}) if $curbuf->Get(3) =~ /someKeyword\((?'outer'(?'inner'"(?:\\.|[^"])*"|'(?:[^']|'')*'|[^()]*|\((?P>inner)*\))*)\)/
    

    Note that if can avoid such regular expressions, you should do it (because you depend on re compiler too much), so I suggest to use vim motions directly:

    let s:reply=""
    function! SetReplyToKeywordArgs(...)
        let [sline, scol]=getpos("'[")[1:2]
        let [eline, ecol]=getpos("']")[1:2]
        let lchar=len(matchstr(getline(eline), '\%'.ecol.'c.'))
        if lchar>1
            let ecol+=lchar-1
        endif
        let text=[]
        let ellcol=col([eline, '$'])
        let slinestr=getline(sline)
        if sline==eline
            if ecol>=ellcol
                call extend(text, [slinestr[(scol-1):], ""])
            else
                call add(text, slinestr[(scol-1):(ecol-1)])
            endif
        else
            call add(text, slinestr[(scol-1):])
            let elinestr=getline(eline)
            if (eline-sline)>1
                call extend(text, getline(sline+1, eline-1))
            endif
            if ecol<ellcol
                call add(text, elinestr[:(ecol-1)])
            else
                call extend(text, [elinestr, ""])
            endif
        endif
        let s:reply=join(text, "\n")
    endfunction
    function! GetKeywordArgs()
        let winview=winsaveview()
        keepjumps call search('someKeyword', 'e')
        setlocal operatorfunc=SetReplyToKeywordArgs
        keepjumps normal! f(g@i(
        call winrestview(winview)
        return s:reply
    endfunction
    

    You can use something like

    let savedureg=@"
    let saved0reg=@0
    keepjumps normal! f(yi(
    let s:reply=@"
    let @"=savedureg
    let @0=saved0reg
    

    instead of operatorfunc to save and restore registers, but the above code leaves all registers and marks untouched, what I can’t guarantee with saved* stuff. It also guarantees that if you remove join() around text, you will save information about the location of NULLs (if you care about them, of course). It is not possible with registers variant.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I'm trying to create an if statement in PHP that prevents a single post
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.