0

I have a line in a text file which is as follows:

0 {1.H1'} 5.82020 0.05000 0.10000 ++ {0.0} {} {2.H8} 7.61004 0.05000 0.10000 ++ {0.0} {} 0.0 100.0000 0 {} 0 0 0

The code I used for picking out the values 5.82020 and 7.61004 are as follows:

float_num = re.findall("[\s][1-9]{1}\.[0-9]+",line)

If instead I wanted to pick out the words 1.H1' and 2.H8, what should I change in my code?

Also, there are multiple lines in my text file, I just picked one out as an example.

Qeek
  • 1,642
  • 17
  • 23
user8290579
  • 189
  • 10

1 Answers1

0

A big part of writing a nice regex is being able to describe and see the actual pattern. In this case you would like:

{1.H1'}

and

{2.H8}

There isn't anything nice like value={blah} for you to grab so I like to start with the literal of what I'm going for:

thelist = re.findall("{2.H8}",line)

And then make it more generic to get the matches I really want (and escape the special characters), like this:

thelist = re.findall("\{\d\.H\d\}",line)

Cool. Note you don't really need to escape the { or } but I usually do in case you end up with a number in there, then it would have a special meaning. However we are missing the {1.H1'} so we need an optional ' like this:

thelist = re.findall("\{\d\.H\d'?\}",line)

Then if you find you are missing some, adjust as you see fit to make the pattern broader.

sniperd
  • 4,243
  • 6
  • 22
  • 36
  • Thank you! So, if I wanted to add another special character, for example if my line included {1.H3'$}, then would I change it to re.findall("\{\d\.H\d'?\d$?\}",line)? – user8290579 Jul 24 '17 at 13:43
  • $ has a special meaning so it needs to be escaped. The \d means a number. So for {1.H3'$} you would just want so to adjust like this: \{\d\.H\d'?\$?\} The ? means that the previous character is optional. So this regex means that there might be a $ – sniperd Jul 24 '17 at 13:47
  • There are all kinds of resources as to what does "this" regex mean? Here is my favorite one: https://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean give that a look and you'll have a whole new pile of tools to write this stuff :) – sniperd Jul 24 '17 at 13:48
  • I see, I'll definitely look through it, thanks again! – user8290579 Jul 24 '17 at 13:51