-1

I am trying to look for a string of the form '[123]' in a given string but I'm not expecting the result I'd want to see i.e. '[123]'.

line = '[123] some string'
words = line.split()
for word in words:
    id = re.sub("\[(\d+)\]", "", word)
    print id

This gives me the output:

empty space
some
string

I'm unsure why re.sub() is formatting my correctly matched string '[123]' this way.

robinhood91
  • 1,641
  • 3
  • 20
  • 36
  • why wouldn't it? you are iterating through every word and substituting any occurences of `[a bunch of numbers]` with an empty string then printing that word with a new line (reminder that `print` adds a newline after unless you put a comma like `print this,`. what are you trying to do? – R Nar Dec 01 '15 at 18:07
  • I guess I'm not using re.sub() properly -- what should be the second argument for re.sub() if I were trying to find the pattern '[number]' ? – robinhood91 Dec 01 '15 at 18:11
  • You are replacing in each word, so the first one is removed completely, the second and third ones are not modified and printed as is. You needn't split the string, just use [`print(re.sub(r"\[(\d+)\]", "", line))`](http://ideone.com/puTuRN). – Wiktor Stribiżew Dec 01 '15 at 18:15
  • what do you want to be doing? if you are just looking to match and find, you should be using `re.match` or `re.search` and accessing `group(1)` – R Nar Dec 01 '15 at 18:16

2 Answers2

0

The output it's correct, i think you want something like this:

line = '[123] some string'
print re.search("\[\d+\]", line).group(0)

Output: [123]

Giuseppe Ricupero
  • 6,134
  • 3
  • 23
  • 32
0

maybe this could help you!

#!/usr/bin/env python -u
# -*- coding: utf-8 -*-

import re

line = '[123] some string'
words = line.split()
for word in words:
    pm = re.search('(\[.*?\])',word)
    if pm is not None:
        print pm.group(0)

output:

python test.py                                           ] 3:13 
[123]
nguaman
  • 925
  • 1
  • 9
  • 23