python在字符串的末尾找到特定的模式

时间:2016-06-17 17:06:08

标签: python regex

我需要在字符串的末尾找到时间戳模式。例如,blahblahblah_tsYYYMMDDHHMMSS。我需要提取时间字符串并将其替换为当前时间戳。 这种模式的正则表达式是什么?

2 个答案:

答案 0 :(得分:0)

谢谢@ rock321987,我这样做了:

import Close from '../../icons/Close'
import Delete from '../../icons/Delete'
import Edit from '../../icons/Edit'
import List from '../../icons/List'
import AccountSimple from './AccountSimple'
import '../../../../css/accounts.css'

class AccountPage extends React.Component{
    constructor(props){
        super(props);
        this.state = {
            data: null,
            first_name: '',
            account: '',
            address: '',
            tableBody: undefined

        };

    }

    componentDidMount() {
        this.tableBody = document.getElementById("table-body");
        this.serverRequest = $.get("/api/client.json", function (result) {
          var client = result;
          this.setState({
             data: client,
          });
        }.bind(this));

    }

    componentDidUpdate() {
        // logs all objects from ajax
        console.log(data); 
    }

    render() {

        return (
            <div className="contactto-middle-content">
            <h1 className="account-title">Usuários</h1>
            <div className="account-table">
              <div className="account-table-row account-table-row-header">
                  <div className="account-table-row-header-td account-table-td-name">Nome</div>
                  <div className="account-table-row-header-td account-table-td-account">Conta</div>
                  <div className="account-table-row-header-td account-table-td-address">Endereço</div>
                  <div className="account-table-row-header-td account-table-td-actions">Ações</div>
              </div>
              <div id="table-body" className="account-table-row account-table-row-body">

              </div>
            </div>
            </div>

        );
    }
}

export default AccountPage

效果很好。

答案 1 :(得分:0)

您可以尝试制作一个功能。

def tsExtractor(SomeString):        #your String
    LenSomeString = len(SomeString) #get length of String
    TargetString = SomeString[(LenSomeString - 14):LenSomeString] #Get placeholder of numeric data
    try:
        int(TargetString) #attempt to change to integer
    except ValueError:
        TargetString = 99998877665544  #not integer, put in some dummy data
    return str(TargetString) #change integer back to string
#examples
exampleString = 'blahblahblahyeahblahblahts20160616140003'
badString = 'Blahblahblahaldskfjlakdflaksdflkjasl500566'

exampleString = tsExtractor(exampleString)
print(exampleString)

exampleString = tsExtractor(badString)
print(exampleString)