有没有办法获取所有字符串(包括其他符号,如-,) 在给定的特定符号(在这种情况下为“ @”符号)之前?
示例 domain \ username @ abc 我想获取“ 域\用户名”,因此我使用表达式(。+)@ ,它可以正常工作。
但是同时会有一些输入巫师,它们没有@abc只是“ domain \ username”,因此不需要拆分,(。+)将起作用
但是无法找出可以同时匹配两者的正则表达式,
在同一小组中
我的解决方案是在regex表达式之前添加if-else,以便它可以确定字符串包含@符号,因此我可以在两种情况下应用不同的表达式。
我尝试过
答案 0 :(得分:2)
您可以使用
enum ETime {
Seconds = 1000,
Minutes = 60000,
Hours = 3600000,
SecInMin = 60,
MinInHours = 60,
HoursMod = 24,
timeMin = 10,
}
interface ITime {
millis: number
modulo: number
}
const Times = {
seconds: {
millis: ETime.Seconds,
modulo: ETime.SecInMin,
},
minutes: {
millis: ETime.Minutes,
modulo: ETime.MinInHours,
},
hours: {
millis: ETime.Hours,
modulo: ETime.HoursMod,
},
}
const dots: string = ":"
const msToTime = (duration: number, needHours: boolean = true): string => {
const getCorrectTime = (divider: ITime): string => {
const timeStr: number = Math.floor(
(duration / divider.millis) % divider.modulo,
)
return timeStr < ETime.timeMin ? "0" + timeStr : String(timeStr)
}
return (
(needHours ? getCorrectTime(Times.hours) + dots : "") +
getCorrectTime(Times.minutes) +
dots +
getCorrectTime(Times.seconds)
)
}
请参见regex demo
它将在字符串(2019-03-18T14:57:06.683+0100 I REPL_HB [replexec-0] Error in heartbeat (requestId: 3) to 10.100.xxx.xxx:27117, response status: NetworkInterfaceExceededTimeLimit: Couldn't get a connection within the time limit
2019-03-18T14:57:12.683+0100 I ASIO [Replication] Connecting to 10.100.60.138:27117
2019-03-18T14:57:14.852+0100 I NETWORK [listener] connection accepted from 10.100.xxx.xxx:53844 #15 (11 connections now open)
2019-03-18T14:57:14.852+0100 I NETWORK [conn15] received client metadata from 10.100.xxx.xxx:53844 conn15: { driver: { name: "MongoDB Internal Client", version: "4.0.4" }, os: { type: "Linux", name: "Ubuntu", architecture: "x86_64", version: "16.04" } }
)的开头匹配^[^@]+
(@
)以外的一个或多个字符。