I have strings such as
我有字符串
(GMT -4:00)Puerto Rico
(GMT -3:30)Newfoundland
(GMT -3:00)Asuncion
(GMT +2:00)Athens
How can I extraxt the time from those string? This was my poor shot: \([0-9](.*?)\)
Result should look like this: -4:00, -3:30, -3:00...
我怎么能从这些字符串中提取时间呢?这是我糟糕的投篮:([0-9](.*?)\)结果应该是这样的:-4:00,-3:30,-3:00…
I am very bad at this.
我很不擅长这个。
1 个解决方案
#1
3
You may use a mere
你可以使用
[-+]\d+:\d+
See the regex demo
看到regex演示
Details:
细节:
-
[-+]
- matches-
or+
- [-+]-匹配-或+。
-
\d+
- 1 or more digits - \d+ - 1或更多的数字
-
:
- a colon - :-一个冒号
-
\d+
- 1 or more digits. - \d+ - 1或更多的数字。
C#:
c#:
var results = Regex.Matches(s, @"[-+]\d+:\d+", RegexOptions.ECMAScript)
.Cast<Match>()
.Select(m => m.Value)
.ToList();
A possible alternative non-regex solution to process each separate input:
一个可能的非regex解决方案来处理每个单独的输入:
var s = "(GMT -4:00)Puerto Rico";
var res = s.Split(new[] {" ", ")"}, StringSplitOptions.RemoveEmptyEntries)
.Skip(1)
.FirstOrDefault();
#1
3
You may use a mere
你可以使用
[-+]\d+:\d+
See the regex demo
看到regex演示
Details:
细节:
-
[-+]
- matches-
or+
- [-+]-匹配-或+。
-
\d+
- 1 or more digits - \d+ - 1或更多的数字
-
:
- a colon - :-一个冒号
-
\d+
- 1 or more digits. - \d+ - 1或更多的数字。
C#:
c#:
var results = Regex.Matches(s, @"[-+]\d+:\d+", RegexOptions.ECMAScript)
.Cast<Match>()
.Select(m => m.Value)
.ToList();
A possible alternative non-regex solution to process each separate input:
一个可能的非regex解决方案来处理每个单独的输入:
var s = "(GMT -4:00)Puerto Rico";
var res = s.Split(new[] {" ", ")"}, StringSplitOptions.RemoveEmptyEntries)
.Skip(1)
.FirstOrDefault();