Java中用于密码模式的正则表达式

时间:2019-05-28 10:12:53

标签: java regex

该正则表达式不适用于“至少一个字母,至少一个数字和至少一个特殊字符”和“至少一个数字和至少一个特殊字符”

例如:

class App extends React.Component {
constructor(props) {
    super(props);
    this.state = {
        data: [],
        library: null,
        perPage: 20,
        currentPage: 1,
        maxPage: null,

    }
}
componentDidMount() {
    fetch('/json.bc', {
        method: 'get',
    })
        .then(response => response.text())
        .then(text => {
            const Maindata = JSON.parse(text.replace(/\'/g, '"'))
            const type1  = Maindata.find(({ type }) => type === '1');
            const MergedData= Maindata.map(item => item.type === '1' ? item : { ...type1, ...item });
            const MergedData2 = MergedData.map(item => item.type === '1' ? item : {...item.hotels}); /// Here I write this code to show just the objects of Infoes array///////
            this.setState(state => ({
                ...state,
                data: MergedData2
            }), () => {
                this.reorganiseLibrary()
            })
        }).catch(error => console.error(error))

}

reorganiseLibrary = () => {
    const { perPage, data } = this.state;
    let library = data;
    library = _.chunk(library, perPage);
    this.setState({
        library,
        currentPage: 1,
        maxPage: library.length === 0 ? 1 : library.length
    })
}

// Previous Page
previousPage = event => {
    this.setState({
        currentPage: this.state.currentPage - 1
    })
}
// Next Page 
nextPage = event => {
    this.setState({
        currentPage: this.state.currentPage + 1
    })
}

// handle per page
handlePerPage = (evt) =>
    this.setState({
        perPage: evt.target.value
    }, () => this.reorganiseLibrary());

// handle render of library
renderLibrary = () => {
    const { library, currentPage } = this.state;
    if (!library || (library && library.length === 0)) {
        return <div>NOResult</div>
    }
    return library[currentPage - 1].map((item, i) => (
        <div className="Wrapper">{this.renderInfo(item)}</div>
    ))
}

renderInfo(element) {
    let len = element.Infos.length
    for (let i = 0; i < len; i++) {
        return element.Infos[i].total
    }
}

render() {
    const { library, currentPage, perPage, maxPage } = this.state;
    return (
        <div>
            {this.renderLibrary()}
            <ul id="page-numbers">
                <li className="nexprevPage">
                    {currentPage !== 1 && (
                        <button onClick={this.previousPage}><span className="fa-backward"></span></button>
                    )}
                </li>
                <li className="controlsPage activeCnt">{this.state.currentPage}</li>
                <li className="restControls">...</li>
                <li className="controlsPage">{this.state.maxPage}</li>
                <li className="nexprevPage">
                    {(currentPage < maxPage) && (
                        <button onClick={this.nextPage}><span className="fa-forward"></span></button>
                    )}
                </li>
            </ul>
        </div>
    )
}
}
ReactDOM.render(<App />, document.getElementById('Result'))

注意:应至少检查一个字母,数字和特殊字符

String passwordpattern="A9009"; //Not working for  pattern3

注意:它应至少检查一个特殊字符和至少一位数字

String passwordpattern="A3566523"; //Not working for  pattern4

2 个答案:

答案 0 :(得分:3)

对于这些类型的断言,最好使用lookahead assertions

  

“至少一个字母,至少一个数字和至少一个特殊字符”

^(?=.*\pL)(?=.*\d)(?=.*\W).+$

\pL匹配任何unicode字母,\d匹配任何数字,\W匹配任何非单词字符。

  

至少一个特殊字符和至少一位数字

^(?=.*\d)(?=.*\W).+$

请注意,在使用matches方法时,无需使用锚点。

答案 1 :(得分:0)

此正则表达式模式将满足您的要求-仅在给定的字符串输入中至少有一个字母,一个数字和一个特殊字符时匹配:

^(?=.)[a-zA-Z]+[0-9]+[^\w]+[^\s]+