匹配可选的String正则表达式(Matching optional String regex)
我必须为我的小问题找到解决方案: - /。
我有一条消息,例如
状态:好的
要么
状态:ERR |下一条消息
是否可以为“状态”创建正则表达式+可选的消息如果存在?
非常感谢
刘若英
i have to find a solution for my little Problem :-/.
I have a message eg
Status: OK
or
Status: ERR|next Message
is it possible to create a regex for "Status" + optional the Message if exists ?
Thanks a lot
Rene
最满意答案
你可以使用这样的东西:
/^Status:\s*([A-Z]+)(?:\|([^$]*))?$/这匹配所有数据,并返回OK或ERR作为第一个元素,只返回消息作为第二个元素。
附加信息 :(?:表示法(在\ |之前),是一个“隐藏”分组。即组,但不提取。
You can use something like this:
/^Status:\s*([A-Z]+)(?:\|([^$]*))?$/This matches all the data, and returns OK or ERR as the first element, and only the message as the second element.
Additional information: The (?: notation (before the \|), is a "hidden" grouping. That is, group but don't fetch.
更多推荐
发布评论