首页 \ 问答 \ Python的正则表达式几小时到几分钟(Python regex hours to minutes)

Python的正则表达式几小时到几分钟(Python regex hours to minutes)

我有几个小时写的字符串,我想转换为分钟

一些示例输入字符串:

50 hour
5 hrs
3 hours
5hrs
1hour

我试图找出一些正则表达式来捕获所有这些,并将整个事件转换为一个整数,即分钟值

以上将变成:

3000
300
180
300
60

我不太熟悉正则表达式。 我开始玩这样的东西:

re.sub(r'^\d+[a-z]', 'blah', string)

然而,这似乎并没有吸引太多。 只有一个数字后跟一个字母的情况。 此外,我不确定如何实际将转换转换为分钟


I have a bunch of strings written in hours that I want converted to minutes

Some example input strings:

50 hour
5 hrs
3 hours
5hrs
1hour

I'm trying to come up some regex that will capture all of them and convert the whole thing to an integer that the minute value

The above will turn into:

3000
300
180
300
60

I'm not too familiar with regex. I started playing around with something like this:

re.sub(r'^\d+[a-z]', 'blah', string)

However that doesn't appear to catch much at all. Only situations where theres a single digit followed by a letter. Furthermore, I'm not sure how to actually do the conversion into minutes


原文:https://stackoverflow.com/questions/34305492
更新时间:2022-09-14 21:09

最满意答案

使用一个类而不是一个id:

.hide-row { display:none; }

并在您的HTML / PHP文件中:

<table>
  <tr>
      <th>Name</th>
      <th>Address</th>
  </tr>
     <?php foreach( $cops as $row ) { ?>
        <tr class="hide-row">
            <td><?php echo $row->name; ?></td>
            <td><?php echo $row->address; ?></td>
        </tr>
     <?php } ?>
</table>

如果你必须将你的行分组,你可以使用tbody标签而不是div标签。

我们可以在同一个<table>中有多个<tbody>吗?

 .hide-row tr { display:none; }

并在您的HTML / PHP文件中:

<table>
  <tr>
      <th>Name</th>
      <th>Address</th>
  </tr>
    <tbody class="hide-row">
     <?php foreach( $cops as $row ) { ?>
        <tr>
            <td><?php echo $row->name; ?></td>
            <td><?php echo $row->address; ?></td>
        </tr>
     <?php } ?>
     </tbody>
</table>

Use a class instead of an id:

.hide-row { display:none; }

And in your html/php file:

<table>
  <tr>
      <th>Name</th>
      <th>Address</th>
  </tr>
     <?php foreach( $cops as $row ) { ?>
        <tr class="hide-row">
            <td><?php echo $row->name; ?></td>
            <td><?php echo $row->address; ?></td>
        </tr>
     <?php } ?>
</table>

If you have to group your rows you could use a tbody tag instead of a div tag.

Can we have multiple <tbody> in same <table>?

 .hide-row tr { display:none; }

And in your html/php file:

<table>
  <tr>
      <th>Name</th>
      <th>Address</th>
  </tr>
    <tbody class="hide-row">
     <?php foreach( $cops as $row ) { ?>
        <tr>
            <td><?php echo $row->name; ?></td>
            <td><?php echo $row->address; ?></td>
        </tr>
     <?php } ?>
     </tbody>
</table>

相关问答

更多