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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:41:44+00:00 2026-05-17T18:41:44+00:00

I have the following string: schema(field1, field2, field3, field4 … fieldn) I need to

  • 0

I have the following string:

schema(field1, field2, field3, field4 ... fieldn)

I need to transform the string to an object with name attribute as schema and the field names as another attribute which is a list.

How do I do this in Python with a regular expression?

  • 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-17T18:41:44+00:00Added an answer on May 17, 2026 at 6:41 pm

    Regular expressions for things like that probably need tests:

    import unittest
    
    import re
    
    # Verbose regular expression!  http://docs.python.org/library/re.html#re.X
    p = r"""
    
    (?P<name>[^(]+)         # Match the pre-open-paren name.
    \(                      # Open paren
    (?P<fields>             # Comma-separated fields
        (?:
            [a-zA-Z0-9_-]+
            (?:,\ )         # Subsequent fields must separated by space and comma
        )*
        [a-zA-Z0-9_-]+       # At least one field. No trailing comma or space allowed.
    )
    
    \)                      # Close-paren
    """
    
    # Compiled for speed!
    cp = re.compile(p, re.VERBOSE)
    
    class Foo(object):
        pass
    
    
    def validateAndBuild(s):
        """Validate a string and return a built object.
        """
        match = cp.search(s)
        if match is None:
            raise ValueError('Bad schema: %s' % s)
    
        schema = match.groupdict()
        foo = Foo()
        foo.name = schema['name']
        foo.fields = schema['fields'].split(', ')
    
        return foo
    
    
    
    class ValidationTest(unittest.TestCase):
        def testValidString(self):
            s = "schema(field1, field2, field3, field4, fieldn)"
    
            obj = validateAndBuild(s)
    
            self.assertEqual(obj.name, 'schema')
    
            self.assertEqual(obj.fields, ['field1', 'field2', 'field3', 'field4', 'fieldn'])
    
        invalid = [
            'schema field1 field2',
            'schema(field1',
            'schema(field1 field2)',
            ]
    
        def testInvalidString(self):
            for s in self.invalid:
                self.assertRaises(ValueError, validateAndBuild, s)
    
    
    if __name__ == '__main__':
        unittest.main()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following simple shema: var userSchema = new Schema({ name : String,
I have the following schema.yml file: Page: actAs: I18n: fields: [name,html,urlShortDesc] columns: name: string
I have the following solr fields in schema.xml: id (string) name (string) category(string) latitude
I have the following Schemas in Mongoose/Node.js: var ItemsSchema = new Schema({ itemname: String,
We have the following Solr (3.4) schema for indexing html/text documents: <fields> <field name=text
I have a table name Discount that has the following schema: PK DiscountID int
I have the following field definition in my SOlr Schema name=DvdReleaseDate type=date stored=true required=false
Let's say I have the following schema var userSchema = new Schema({ name :
I have following string: Test, User < test@test.com >, Another, Test < another@test.com >,
I have following situation: String a = A Web crawler is a computer program

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.