How do I make c开发者_开发百科ertain portion of a regular expression optional? For example:
\d* [\\s\\w*?]
...where the expression inside the brackets is optional.
I don't think that's quite the regular expression you want. [\s\w*?] says "match one character that is a space, a word, a star, or a question mark". I think you ultimately what you want is (\s\w)*? which says "match 0 or more of a space followed by a word and don't be greedy about it.
Though you could be looking for (\s|\w)*? which says "match a word or a space 0 or more times and don't be greedy about it."
Just add a * after the ]. * means 0 or more.
You can make a bracketed group optional, so (abc)? would work as expected. \d*(\s\w*?)? I think would do what you're describing
Add ? after the ]. The ? means zero or one of the preceding element.
加载中,请稍侯......
精彩评论