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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:59:57+00:00 2026-05-26T05:59:57+00:00

I have a simple line of text which might include numbers like 12.3 or

  • 0

I have a simple line of text which might include numbers like “12.3” or “1983” or “5/8”.
Whenever any number appears, I just need to replace with a fixed character, say the digit “8”.

I’ve been fiddling about with Regex in Java, with things like this:

String line = str.replaceAll("[0-9]+/*.*[0-9]*", "8");

but to no avail.

Any idea what the correct pattern should 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-26T05:59:57+00:00Added an answer on May 26, 2026 at 5:59 am

    Try this expression: (?>-?\d+(?:[\./]\d+)?), keep in mind that in Java strings you need to escape the backslashes, i.e. you’d get "(?>-?\\d+(?:[\\./]\\d+)?)"

    Here’s a breakdown of the expression:

    1. The encloseing (?>...) is an atomic group to prevent catastrophic backtracking. For simple or short strings it would work without as well.

    2. -? a potential minus for negative numbers

    3. \d+ any sequence of digits (at least one)

    4. (?:[\./]\d+)? an optional non-capturing group consisting of either a dot (note that you don’t need to escape it here, it’s just for consistency) or a slash followed by at least one more digit.

    Update

    If you don’t want to replace “numbers” like .1234, 1234. /1 or 5/ (a digit is missing either left or right), try this expression: (?>(?<![\d\./])-?\d+(?:(?:[\./]\d+)|(?![\d\./])))

    Here’s a breakdown again:

    1. The encloseing (?>...) is an atomic group to prevent catastrophic backtracking. For simple or short strings it would work without as well.

    2. (?<![\d\./]) the match must not directly follow a digit, dot or slash – note that the not follow a digit constraint is needed to match at the start of the number, otherwise you’d match 234 in .1234

    3. -? a potential minus for negative numbers

    4. \\d+ any sequence of digits (at least one)

    5. (?:(?:[\./]\d+)|(?![\d\./])) the match must either have a dot or slash followed by at least one digit or must not be followed by a digit, dot or slash, this would match 1.0 but not 1. – note that the not to be followed by a digit constraint is needed to prevent matching 123 in 1234.

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

Sidebar

Related Questions

I have made a simple Line Chart: Which is showing 4 grid-lines at points
I have a simple JEditorPane inside a JScrollPane that displays line numbers on the
I have a simple application which basically consists of a line of buttons and
I have a simple list, which I managed to get in a line and
I have a simple <input type=text/> styled with the following: font-size:1.5em;line-height:1.5em;padding:.6em .4em; It displays
I have a edit text which is multi line (which saves more than 1
I have a simple java code which gets html text from the input url:
let's assume I have a text file which acts as a simple database by
I have a simple WrapPanel which contains a number of wide controls. When I
I have a simple JSF line chart that uses PrimeFaces (via jqPlot) library: <p:lineChart

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.