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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:50:31+00:00 2026-05-11T18:50:31+00:00

I don’t know if that’s the right word for it, but I am trying

  • 0

I don’t know if that’s the right word for it, but I am trying to come up with some regex’s that can extract coefficients and exponents from a mathematical expression. The expression will come in the form ‘axB+cxD+exF’ where the lower case letters are the coefficients and the uppercase letters are the exponents. I have a regex that can match to both of them, but I’m wondering if I can use 2 regexs, one to match the coefficients and one for the exponents. Is there a way to match a number with a letter on one side of it without matching the letter? EG, in ‘3×3+6×2+2×1+8×0’ I need to get
[‘3’, ‘6’, ‘2’, ‘8’]
and
[‘3’, ‘2’, ‘1’, ‘0’]

  • 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-11T18:50:32+00:00Added an answer on May 11, 2026 at 6:50 pm

    You can use positive look-ahead to match something that is followed by something else. To match the coefficients, you can use:

    >>> s = '3x3+6x2+2x1+8x0'
    >>> re.findall(r'\d+(?=x)', s)
    ['3', '6', '2', '8']
    

    From the documentation of the re module:

    (?=…)
    Matches if … matches next, but doesn’t consume any of the string.
    This is called a lookahead assertion.
    For example, Isaac (?=Asimov) will
    match ‘Isaac ‘ only if it’s followed
    by ‘Asimov’.

    For the exponents, you can use positive look-behind instead:

    >>> s = '3x3+6x2+2x1+8x0'
    >>> re.findall(r'(?<=x)\d+', s)
    ['3', '2', '1', '0']
    

    Again, from the docs:

    (?<=…) Matches if the current position in the string is preceded by a match for
    … that ends at the current position.
    This is called a positive lookbehind
    assertion. (?<=abc)def will find a
    match in abcdef, since the lookbehind
    will back up 3 characters and check if
    the contained pattern matches.

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

Sidebar

Related Questions

don't know if the title describes anything about what I'm trying to say but
Don't know if this is the right place to ask this, but I will
Don't know if this is possible, but I have some code like this: val
Don't know if I'm over-thinking this or not.. but I'm trying to be able
Don't know why but I can't find a solution to this. I have 3
DON'T ASK WHY but... I have a regex that needs to be case insensitive
Don't know a better title but here is what im trying to do. I
Don't know what's going on, but for some reason my background image is not
Don't know if I worded the question right, but basically what I want to
Don't know how to explain it better but i'm trying to get a response

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.