简体   繁体   中英

Regex match an optional number of digits

I have a list that could look sort of like

("!Goal 27' Edward Nketiah"),
("!Goal 33' 46' Pierre Emerick-Aubameyang"),
("!Sub Nicolas Pepe"),
("Jordan Pickford"),

and I'm looking to match either !Sub or !Goal 33' 46' or !Goal 27'

Right now I'm using the regex (!\w+\s) which will match !Goal and !Sub , but I want to be able to get the timestamps too. Is there an easy way to do that? There is no limit on the number of timestamps there could be.

As I mentioned in my comment, you can use the following regex to accomplish this:

(!\w+(?:\s\d+')*)

Explanation:

  • (?\w+(::\s\d+')*) capture the following
    • ! matches this character literally
    • \w+ matches one or more word characters
    • (?:\s\d+')* match the following non-capture group zero or more times
      • \s match a whitespace character
      • \d+ matches one or more digits
      • ' match this character literally

Additionally, the first capture group isn't necessary - you can remove it to simply match:

!\w+(?:\s\d+')*

If you need each timestamp, you can use !\w+(\s\d+')* and split capture group 1 on the space character.

If your input always follows the format "bang text blank digits apostrophe blank digits apostrophe etc", then it should be as simple as:

!\w+(?:\s\d+')*

Explanation:

!      matches an exclamation mark
\w+    matches 1 or more word-characters (letters, underscores)
(?:…)  is a non-capturing group
\s     matches a single whitespace character
\d+    matches one or more digits
'      matches the apostrophe character
*      repeatedly matches the group 0 or more times

this:

(!\w+(?:\s\d+')*)

will capture:

"!Goal 27'"
"!Goal 33' 46'"
"!Sub"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM