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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:42:23+00:00 2026-05-24T13:42:23+00:00

I need to find and replace version numbers within text with a generic placeholder

  • 0

I need to find and replace version numbers within text with a generic placeholder e.g. ‘*’.

Problem is writing the regex that would capture the version numbers.


Some examples:

Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.1 (KHTML, like Gecko) Ubuntu/11.04 Chromium/14.0.825.0 Chrome/14.0.825.0 Safari/535.1

Mozilla/5.0(iPad; U; CPU iPhone OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B314 Safari/531.21.10gin_lib.cc

Mozilla/5.0 (Windows; U; Windows NT 5.1; pt-PT; rv:1.9.2.7) Gecko/20100713 Firefox/3.6.7 (.NET CLR 3.5.30729)

Version numbers contain:

  • alphanumeric characters
  • special characters i.e. ‘.-_:’

A simple regex might be r'[0-9._:-]+' but this does not work as version number needs at least 1 alphanumeric chars and special character in between alphanumeric characters.


Any ideas?

  • 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-24T13:42:23+00:00Added an answer on May 24, 2026 at 1:42 pm

    In the re module, use the sub function. It will return a string where all the matches for the input regex are replaced by the output of a function, or just a string. The problem is in determining which version numbers in each string you want to replace. I’m assuming that you want all version numbers replaced.

    import re
    data = ["Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.1 (KHTML, like Gecko) Ubuntu/11.04 Chromium/14.0.825.0 Chrome/14.0.825.0 Safari/535.1",
    "Mozilla/5.0(iPad; U; CPU iPhone OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B314 Safari/531.21.10gin_lib.cc",
    "Mozilla/5.0 (Windows; U; Windows NT 5.1; pt-PT; rv:1.9.2.7) Gecko/20100713 Firefox/3.6.7 (.NET CLR 3.5.30729)"]
    output = []
    for str in data:
       output.append(re.sub(r'\d[0-9a-zA-Z._:-]+', '*', str))
    print output
    

    gives these results:

    ['Mozilla/* (X*; Linux i*) AppleWebKit/* (KHTML, like Gecko) Ubuntu/* Chromium/* Chrome/* Safari/*', 
    'Mozilla/*(iPad; U; CPU iPhone OS * like Mac OS X; en-us) AppleWebKit/* (KHTML, like Gecko) Version/* Mobile/* Safari/*',
    'Mozilla/* (Windows; U; Windows NT *; pt-PT; rv:*) Gecko/* Firefox/* (.NET CLR *)']
    

    The regex isn’t very good, I wanted a repeating set of alphanumerics followed by a delimiter. But I couldn’t seem to get it to work. Something like ([0-9a-zA-Z]+[._:-])+

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

Sidebar

Related Questions

I need to do a regex find and replace on all the files in
Right, my problem is that I need to replace an image that appears on
I need to perform a find and replace using XSLT 1.0 which is really
I need to do a find and replace in Notepad++ of Err.Number, canBeAnything, canBeAnything,
I need to find two adjacent repeating digits in a string and replace with
I need to find out how to format numbers as strings. My code is
I need to find/create an application that will create employee web usage reports from
I need to find and replace an element in an array. Is there any
I need help while trying to spin articles. I want to find text and
Is there any PHP PDF library that can replace placeholder variables in an existing

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.