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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:08:11+00:00 2026-05-25T23:08:11+00:00

I have such string: something: 20 kg/ something: 120 kg I have this regex

  • 0

I have such string:

something: 20 kg/ something: 120 kg

I have this regex ("[0-9]{1,2} kg", string), but it returns 20kg both times. I need to return 20kg only in first case.

  • 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-25T23:08:12+00:00Added an answer on May 25, 2026 at 11:08 pm

    Try this:

    (?<!\d)\d{1,2}\s+kg
    

    The (?<!...) is a negative look behind. So it matches one or two digits not preceded by a digit. I also changed the literal space with one or more space chars.

    Seeing you’ve asked Python questions, here’s a demo in Python:

    #!/usr/bin/env python
    import re
    string = 'something: 20 kg/ something: 120 kg'
    print re.findall(r'(?<!\d)\d{1,2}\s+kg', string)
    

    which will print ['20 kg']

    edit

    As @Tim mentioned, a word boundary \b is enough: r'\b\d{1,2}\s+kg'

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

Sidebar

Related Questions

If I have a string such as lotsofcrapsomethingimportantmoreotherstuff is it possible to get Regex
Lets say a have a string such as this one: string txt = Lore
I have written this code to convert string in such format 0(532) 222 22
Suppose I have a string such as this in a text file: (((var1 AND
I have a string such as: #sometag-{serialized-data-here} And I want to match this pattern
I have a string such as big bad dog, how can I get an
I have such dictionary Dictionary<string, object> , dictionary holds string keys and objects as
I have an input string such as x=y|a=b|c=10 etc which gets converted to dynamic
I have a property such as IdentityCode string IdentityCode {get;set;} my IdentityCode has three
I have a bunch of records with dates formatted as a string such as

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.