Home>
Just touched on regular expressions,
It is now possible to extract specific strings from complex text sentences.
You'll be amazed by the really useful features.

This is the main subject.
It would be nice to extract a specific string, but
I got stuck in the operation from there.

Here is the string.

0 [hour] + 2 [minute]
0 [hour] + 47 [minute]
0 [hour] + 4 [minute]
0 [hour] + 23 [minute]
0 [hour] + 3 [minute]


Suppose we read the above string.
The next goal is to find the total time in minutes.
In this case, the desired value (execution result) is 79.

"" "
'.' Represents an arbitrary character string.
'/ d' represents any number.
Looking for operations that might be helpful from the web,
I tried to write a code once, but the execution result is
"" "
0 [hour] + 2 [minute]
0 [hour] + 47 [minute]
0 [hour] + 4 [minute]
0 [hour] + 23 [minute]
0 [hour] + 3 [minute]
0 [hour] + 40 [minute]
0
[
h
o
u
r
]
+
Four
0
[
m
i
n
u
t
e
]
# It was a failure.


I do n’t know what I ’m doing.

Added (actual code I wrote)
# processA is read from the plaintext with the with syntax.
for el in processA:
    re.findall ('/ d [minute]', el)
    if el:
        print (el)
for q in el:
    re.findall ('/ d [minute]', q)
    if q:
        print (q)
"" "
I want the X part of X [minute].
An error was also issued by changing 'q' to '/ d' in the code.
"" "
  • Answer # 1

    The number is\ d, not/d.

    import re
    text = "" "
    0 [hour] + 2 [minute]
    0 [hour] + 47 [minute]
    0 [hour] + 4 [minute]
    0 [hour] + 23 [minute]
    0 [hour] + 3 [minute]
    "" "
    result = re.findall (r '\ d + (? = \ [minute])', text)
    print (result)

  • Answer # 2

      

    I don't know what I am doing.

    I'm sure it doesn't do anything meaningful with regular expressions.

    for el in processA:
        re.findall ('/ d [minute]', el) # Since no return value is used, it is the same as not
        if el: # True unless it is an empty string
            print (el)
    for q in el:
        re.findall ('/ d [minute]', q) # Since no return value is used, it is the same as not
        if q: # True unless it is an empty string
            print (q)


    If you rewrite roughly,

    for el in processA: # seems to be reading line by line (processA is probably a file object)
        print (el) # display line by line
    for q in el: # loop the last line displayed one character at a time
        print (q) # display one character at a time

    And not/dbut\ d.