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 6732827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:41:47+00:00 2026-05-26T10:41:47+00:00

I am trying to use regular expressions to match some text. The following pattern

  • 0

I am trying to use regular expressions to match some text.

The following pattern is what I am trying to gather.

@Identifier('VariableA', 'VariableB', 'VariableX', ..., 'VariableZ')

I would like to grab a dynamic number of variables rather than a fixed set of two or three.
Is there any way to do this? I have an existing Regular Expression:

\@(\w+)\W+(\w+)\W+(\w+)\W+(\w+)

This captures the Identifier and up to three variables.

Edit: Is it just me, or are regular expressions not as powerful as I’m making them out to be?

  • 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-26T10:41:47+00:00Added an answer on May 26, 2026 at 10:41 am

    You want to use scan for this sort of thing. The basic pattern would be this:

    s.scan(/\w+/)
    

    That would give you an array of all the contiguous sequences for word characters:

    >> "@Identifier('VariableA', 'VariableB', 'VariableX', 'VariableZ')".scan(/\w+/)
    => ["Identifier", "VariableA", "VariableB", "VariableX", "VariableZ"]
    

    You say you might have multiple instances of your pattern with arbitrary stuff surrounding them. You can deal with that with nested scans:

    s.scan(/@(\w+)\(([^)]+?)\)/).map { |m| [ m.first, m.last.scan(/\w+/) ] }
    

    That will give you an array of arrays, each inner array will have the “Identifier” part as the first element and that “Variable” parts as an array in the second element. For example:

    >> s = "pancakes @Identifier('VariableA', 'VariableB', 'VariableX', 'VariableZ') pancakes @Pancakes('one','two','three') eggs"
    >> s.scan(/@(\w+)\(([^)]+?)\)/).map { |m| [ m.first, m.last.scan(/\w+/) ] }
    => [["Identifier", ["VariableA", "VariableB", "VariableX", "VariableZ"]], ["Pancakes", ["one", "two", "three"]]]
    

    If you might be facing escaped quotes inside your “Variable” bits then you’ll need something more complex.


    Some notes on the expression:

    @            # A literal "@".
    (            # Open a group
      \w+        # One more more ("+") word characters ("\w").
    )            # Close the group.
    \(           # A literal "(", parentheses are used for group so we escape it.
    (            # Open a group.
      [          # Open a character class.
        ^)       # The "^" at the beginning of a [] means "not", the ")" isn't escaped because it doesn't have any special meaning inside a character class.
      ]          # Close a character class.
      +?         # One more of the preceding pattern but don't be greedy.
    )            # Close the group.
    \)           # A literal ")".
    

    You don’t really need [^)]+? here, just [^)]+ would do but I use the non-greedy forms by habit because that’s usually what I mean. The grouping is used to separate the @Identifier and Variable parts so that we can easily get the desired nested array output.

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

Sidebar

Related Questions

I am trying use javascript regular expressions to do some matching and I found
I'm trying to parse some AS3 with the use of Regular Expressions. I cannot,
I'm trying to use regular expressions to match a string that does not contain
I'm trying to match cases with regular expression to search and replace some text
I am trying to use regular expressions to find a UK postcode within a
I'm trying to use grep with -v for invert-match along with -e for regular
I am trying to use regular expressions to make my inventory upload script a
I'm trying to match punctuations using regular expressions. Do I have to specify every
I'm trying to use regular expressions to find three or more of the same
I'm trying to use Java regexps to match a pattern that spans multiple lines.

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.