rollTech69
rollTech69

Reputation: 11

Regex : matching integers inside of brackets

I am trying to take off bracketed ends of strings such as version = 10.9.8[35]. I am trying to substitute the integer within brackets pattern (so all of [35], including brackets) with an empty string using the regex [\[+0-9*\]+] but this also matches with numbers not surrounded by brackets. Am I not using the + quantifier properly?

Upvotes: 1

Views: 2982

Answers (3)

The fourth bird
The fourth bird

Reputation: 163362

You could match the format of the number and then match one or more digits between square brackets.

In the replacement using the first capturing group r'\1'

\b([0-9]+(?:\.[0-9]+)+)\[[0-9]+\]
  • \b Word boundary
  • ( Capture group 1
    • [0-9]+ Match 1+ digits
    • (?:\.[0-9]+)+ Match a . and 1+ digits and repeat that 1 or more times
  • ) Close group
  • \[[0-9]+\] Match 1+ digits between square brackets

Regex demo

For example

import re

regex = r"\b([0-9]+(?:\.[0-9]+)+)\[[0-9]+\]"
test_str = "version = 10.9.8[35]"
result = re.sub(regex, r'\1', test_str)
print (result)

Output

version = 10.9.8

Upvotes: 2

A simpler regex solution:

import re
pattern = re.compile(r'\[\d+\]$')
s = '10.9.8[35]'
r = pattern.sub('', s)
print(r) # 10.9.8

The pattern matches square brackets at the end of a string with one or more number inside. The sub then replaces the square brackets and number with an empty string.

If you wanted to use the number in the square brackets just change the sub expression such as:

import re
pattern = re.compile(r'\[(\d+)\]$')
s = '10.9.8[35]'
r = pattern.sub(r'.\1', s)
print(r) # 10.9.8.35

Alternatively as said by the other answer you can just find it and splice to get rid of it.

Upvotes: 1

JGFMK
JGFMK

Reputation: 8904

No need for regex

s =  '10.9.8[35]'
t = s[:s.rfind("[")]
print(t)

But if you insist ;-)

import re
s =  '10.9.8[35]'
t = re.sub(r"^(.*?)[[]\d+[]]$", r"\1", s)
print(t)

Breakdown of regex:

  • ^ - begins with
  • () - Capture Group 1 you want to keep
  • .*? - Any number of chars (non-greedy)
  • [[] - an opening [
  • \d+ 1+ digit
  • []] - closing ]
  • $ - ends with
  • \1 - capture group 1 - used in replace part of regex replace. The bit you want to keep.

Output in both cases:

10.9.8

Use regex101.com to familiarise yourself more. If you click on any of the regex samples at bottom right of the website, it will give you more info. You can also use it to generate regex code in a variety of languages too. (not good for Java though!).

There's also a great series of Python regex videos on Youtube by PyMoondra.

Upvotes: 1

Related Questions