Rankinstudio
Rankinstudio

Reputation: 612

C# regex match all white space mid string if any there

I'm a bit confused on this one. A few possible strings I am splitting into 7 groups.

 string input = "05 28 55    +52 26 46"; // matches
 string input = "05:28:55.321-52,26,46.1"; // no match
 string input = "05,28,55.32  -52:26:46.1"; // matches

I have a regex creating 7 groups, with different delimiteders possible:

string pattern = @"(\d{2})[\s:,](\d{2})[\s:,](\d{2}?[.]?\d*)?[ \t]+([+-])(\d{2})[\s:,](\d{2})[\s:,](\d{2}?[.]?\d*)";

How can I get the regex to match the space before the + or -, if it is there or not? It works now if there is one or more spaces, but no if there is no space. How can I skip that if there is no space? The ? before [ \t]+ doesn't seem to be working how I thought it would.

Thanks!!

Upvotes: 1

Views: 63

Answers (1)

Rahul Verma
Rahul Verma

Reputation: 3089

Change [ \t]+ to [ \t]* to match 0 or more .

[ \t]+ means one or more
[ \t]* means 0 or more .

Upvotes: 1

Related Questions