Ryan Schlueter
Ryan Schlueter

Reputation: 2221

Regex splitting on period unnecessarily

I am splitting a string using this regex expression.

 inputSplit =  Regex.Split(temp, @"([/+-/*x/÷///²/√])") 

This expression should split on +-*x÷/²√.This works perfectly except when I put in a decimal. If i do 1 + .002 it splits into 1, +, ., 002. Which i cant find out why its splitting the decimal from the .002. I assume its maybe one of my special characters? Thanks

Upvotes: 2

Views: 85

Answers (2)

Dalorzo
Dalorzo

Reputation: 20024

I think you need to revise your expresion probably missing \ next to -

([+\-*x÷²√])

Online Demo

This I think is returning your expected results:

var results = Regex.Split("1+.002", @"([+\-*x÷²√])", RegexOptions.None);

Upvotes: 4

Xiaoy312
Xiaoy312

Reputation: 14477

Your +-/ is matching anything between + to /.

@"([+\-/*x÷²√])"

Upvotes: 2

Related Questions