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

  • Home
  • SEARCH
  • 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 9295579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T21:38:20+00:00 2026-06-18T21:38:20+00:00

I am using the re module in Python(3) and want to substitute (re.sub(regex, replace,

  • 0

I am using the re module in Python(3) and want to substitute (re.sub(regex, replace, string)) a string in the following format

"foo <bar e word> f ga <foo b>" 

to

"#foo <bar e word> #f #ga <foo b>"

or even

"#foo #<bar e word> #f #ga #<foo b>" 

But I can’t isolate single words from word boundaries within a <…> construct.

Help would be nice!

P.S 1

The whole story is a musical one:
I have strings in the Lilypond format (or better, a subset of the very simple core format, just notes and durations) and want to convert them to python pairs int(duration),list(of pitch strings). Performance is not important so I can convert them back and forth, iterate with python lists, split strings and join them again etc.
But for the above problem I did not found an answer.

Source String

"c'4 d8 < e' g' >16 fis'4 a,, <g, b'> c''1"

should result in

[
(4, ["c'"]),
(8, ["d"]),
(16, ["e'", "g'"]),
(4, ["fis'"]),
(0, ["a,,"]),
(0, ["g", "b'"]),
(1, ["c''"]),
]

the basic format is String+Number like so : e4 bes16

  • List item
  • the string can consist of multiple, at least one, [a-zA-Z] chars
  • the string is followed by zero or more digits: e bes g4 c16
  • the string is followed by zero or more ‘ or , (not combined): e’ bes, f”’2 g,,4
  • the string can be substituted by a list of strings, list limiters are <>: 4 The number comes behind the >, no space allowed

P.S. 2

The goal is NOT to create a Lilypond parser. Is it really just for very short snippets with no additional functionality, no extensions to insert notes. If this does not work I would go for another format (simplified) like ABC. So anything that has to do with Lilypond (“Run it trough lilypond, let it give out the music data in Scheme, parse that”) or its toolchain is certainly NOT the answer to this question. The package is not even installed.

  • 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-06-18T21:38:21+00:00Added an answer on June 18, 2026 at 9:38 pm

    Your first question can be answered in this way:

    >>> import re
    >>> t = "foo <bar e word> f ga <foo b>"
    >>> t2 = re.sub(r"(^|\s+)(?![^<>]*?>)", " #", t).lstrip()
    >>> t2
    '#foo #<bar e word> #f #ga #<foo b>'
    

    I added lstrip() to remove the single space that occurs before the result of this pattern. If you want to go with your first option, you could simply replace #< with <.

    Your second question can be solved in the following manner, although you might need to think about the , in a list like ['g,', "b'"]. Should the comma from your string be there or not? There may be a faster way. The following is merely proof of concept. A list comprehension might take the place of the final element, although it would be farily complicated.

    >>> s = "c'4 d8 < e' g' >16 fis'4 a,, <g, b'> c''1"
    >>> q2 = re.compile(r"(?:<)\s*[^>]*\s*(?:>)\d*|(?<!<)[^\d\s<>]+\d+|(?<!<)[^\d\s<>]+")
    >>> s2 = q2.findall(s)
    >>> s3 = [re.sub(r"\s*[><]\s*", '', x) for x in s2]
    >>> s4 = [y.split() if ' ' in y else y for y in s3]
    >>> s4
    ["c'4", 'd8', ["e'", "g'16"], "fis'4", 'a,,', ['g,', "b'"], "c''1"]
    >>> q3 = re.compile(r"([^\d]+)(\d*)")
    >>> s = []
    >>> for item in s4:
        if type(item) == list:
                lis = []
                for elem in item:
                        lis.append(q3.search(elem).group(1))
                        if q3.search(elem).group(2) != '':
                                num = q3.search(elem).group(2)
                if q3.search(elem).group(2) != '':
                        s.append((num, lis))
                else:
                        s.append((0, lis))
        else:
                if q3.search(item).group(2) != '':
                        s.append((q3.search(item).group(2), [q3.search(item).group(1)]))
                else:
                        s.append((0, [q3.search(item).group(1)]))
    
    
    >>> s
    [('4', ["c'"]), ('8', ['d']), ('16', ["e'", "g'"]), ('4', ["fis'"]), (0, ['a,,']), (0, ['g,', "b'"]), ('1', ["c''"])]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Simple enough question: I'm using python random module to generate random integers. I want
I'm trying to implement a python cmd, using the cmd module. I want to
I have a python logger set up, using python's logging module. I want to
I want to get selected text automatically in python, using gtk module. There is
I am using a Python module ( PyCLIPS ) and Django 1.3. I want
I'm using python-requests module to handle oAuth request and response. I want to set
Using Python module re, how to get the equivalent of the \w (which matches
I've compiled a Python module using my own Qt4 library located in ~/opt/qt-4.6.0/ ,
Using the python module xlwt, writing to the same cell twice throws an error:
I am using the urllib2 module in Python 2.6.4, running in Windows XP, to

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.