-1

Related to a former question. I would like to know why the code:

import re
[w for w in g.split() if re.search('^..j..t..$', w)]

does not give an appropriate answer for a text g defined from a previous text file as:

f=open('text.txt')
g=f.read()

For example: take the text: "I would love to give my opinion about the White House, although nobody would listen." and write:

g='I would love t give my opinion about the White House, although nobody would listen.'

in this case when I type

[w for w in g.split() if re.search('^..t..g..$', w)]

the answer is a single '[ ]' and not '[although]' as expected.

Furthermore, can similar commands be executed for searching strings in that manner from a text file?

gibarian
  • 133
  • 6

1 Answers1

0

I have discovered the thing. It is a matter of introducing as much dots as letters the wanted word contains. For example:

[w for w in h if re.search('^..t...g.$', w)]

returns

['although']

but

[w for w in h if re.search('^..t..g..$', w)]

gives nothing. I thought ^ and $ meant whatever before 't' and after 'g' respectively but they not. So, this command is not as useful as I thought.

gibarian
  • 133
  • 6