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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:12:27+00:00 2026-05-13T06:12:27+00:00

Ok… I have an unsatisfactory solution to a problem. The problem is I have

  • 0

Ok… I have an unsatisfactory solution to a problem.

The problem is I have input like so:

{sup 19}F({sup 3}He,t){sup 19}Ne(p){sup 18}F

and need output like so:

¹⁹F(³He,t)¹⁹Ne(p)¹⁸F 

I use a series of replacements first to split each of the {sup xx} sections into {sup x}{sup x} and then use a regex to match each of those and replace the characters with their UTF-8 single equivalents. The “problem” is that the {sup} sections can have numbers 1, 2 or 3 digits long (maybe more, I don’t know), and I want to “expand” them into separate {sup} sections with one digit each. ( I also have the same problem with {sub} for subscripts… )

My current solution looks like this (in java):

retval = retval.replaceAll("\\{sup ([1-9])([0-9])\\}", "{sup $1}{sup $2}");
retval = retval.replaceAll("\\{sup ([1-9])([0-9])([0-9])\\}", "{sup $1}{sup $2}{sup $3}");

My question: is there a way to do this in a single pass no matter how many digits ( or at least some reasonable number ) there are?

  • 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-13T06:12:28+00:00Added an answer on May 13, 2026 at 6:12 am

    Yes, but it may be a bit of a hack, and you’ll have to be careful it doesn’t overmatch!

    Regex:

    (?:\{sup\s)?(\d)(?=\d*})}?
    

    Replacement String:

    {sup $1}
    

    A short explanation:

    (?:                            | start non-capturing group 1
      \{                           |   match the character '{'
      sup                          |   match the substring: "sup"
      \s                           |   match any white space character
    )                              | end non-capturing group 1
    ?                              | ...and repeat it once or not at all
    (                              | start group 1
      \d                           |   match any character in the range 0..9
    )                              | end group 1
    (?=                            | start positive look ahead
      \d                           |   match any character in the range 0..9
      *                            |   ...and repeat it zero or more times
      }                            |   match the substring: "}"
    )                              | stop negative look ahead
    }                              | match the substring: "}"
    ?                              | ...and repeat it once or not at all
    

    In plain English: it matches a single digit, only when looking ahead there’s a } with optional digits in between. If possible, the substrings {sup and } are also replaced.

    EDIT:

    A better one is this:

    (?:\{sup\s|\G)(\d)(?=\d*})}?
    

    That way, digits like in the string "set={123}" won’t be replaced. The \G in my second regex matches the spot where the previous match ended.

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

Sidebar

Ask A Question

Stats

  • Questions 363k
  • Answers 363k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer A window receives this message when the user chooses a… May 14, 2026 at 3:24 pm
  • Editorial Team
    Editorial Team added an answer You can set it up like this: _mockRepos.Setup(x => x.Single<Page>(It.IsAny<Expression<Func<Page,… May 14, 2026 at 3:24 pm
  • Editorial Team
    Editorial Team added an answer +[NSPredicate predicateWithFormat:] is actually the front-end for a rather sophisticated… May 14, 2026 at 3:24 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.