0

I have a two strings:

num = '#123'

line = '#123 random text generator #111 #222 #333'

I wish to get all the numbers of the format '#xyz' if num == first number in line.

I've used regex to get the first number (#123) by:

re.findall(r'[#]\d{3,10}', line)

and I try to test this condition by:

if re.findall(r'[#]\d{3,10}', line)[:1] == num:

I've tried to get the re.findall into a parameter and print its length and type, and it says it's length 0 and type list. It's confusing me because [:1] should give me the '#123' string that it finds right? It seems like the list is empty but I can't figure out why.

More specifically my code has matrix = [['#123'] ['#234'] ['#345'] ['#666']].

def test(matrix,txt):
  for num_group in matrix:
    print num_group
    for num in num_group:
      for line in txt:
        if re.findall(r'[#]\d{3,10}', line)[:1] == num:
          print "found some more numbers in the line number!"
          print line
          more_nums = re.findall(r'[#]\d{3,10}', line)[1:]
          matrix[num_group].append(nums)

So my end result should append #111 #222 and #333 to the matrix[0] that contains #123.

18
  • You should probably add a tag for the language you're using. Commented Dec 18, 2017 at 20:17
  • sry about that :) Commented Dec 18, 2017 at 20:21
  • Can you use the PyPi regex module? You can do it with one pattern then. Or are you limited to Python re? Commented Dec 18, 2017 at 20:31
  • I'm not familiar with PyPi but I want to keep it as simple as possible so I guess no external modules are needed beyond what I've done. It should work also with my code.. Commented Dec 18, 2017 at 20:35
  • 1
    What is num? Is it a string? Right now it looks like a comment. Commented Dec 18, 2017 at 20:57

1 Answer 1

1

You can check via python string inbuilt method 'startswith' and then you can use some logic there , Here is my approach :

import re
pattern=r'#\d+'
num = '#123'

line = '#123 random text generator #111 #222 #333'

matrix = [['#123'],['#234'],['#345'],['#666']]

if line.startswith(num):
    match=re.findall(pattern,line)
    for index,value in enumerate(matrix):
        if match[0] in value:
            value.extend(match[1:])

print(matrix)

output:

[['#123', '#111', '#222', '#333'], ['#234'], ['#345'], ['#666']]

Edit :

As you said you want limit the search for only num then you can add little logic before above code , Here is updated code:

import re
pattern=r'#\d+'
num = '#123'

line = '#123 random text generator #111 #222 #333'

matrix = [['#123'],['#234'],['#345'],['#666']]
if len(line.split()[0])==len(num):
    if line.startswith(num):
        match=re.findall(pattern,line)
        for index,value in enumerate(matrix):
            if match[0] in value:
                value.extend(match[1:])

print(matrix)

Test case 1:

line = '#1234 random text generator #111 #222 #333' #not exact string 

output:

[['#123'], ['#234'], ['#345'], ['#666']] #no effect

Test case 2:

line = '#1234 random text generator #111 #222 #333' #exact string

output:

[['#123', '#111', '#222', '#333'], ['#234'], ['#345'], ['#666']]
Sign up to request clarification or add additional context in comments.

4 Comments

This seems to work better! I'm still having a logical problem when the line startswith '#123' but it's for example '#1231 random text generator #111 #222 #333'. Is there a way to get 'exactly' the value of num?
@YafimSimanovsky its confusing , can you explain what exactly you are looking ?
Your example seems to find the text line in the txt file, but it also finds any line that starts with the same but not exact match. For example lines that start with #1234 will also match the startswith('#123'). Is there a way to limit it somehow to find only the num value?
@YafimSimanovsky i have updated the answer, if my solution helped you, Then you can accept the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.