位置:海鸟网 > IT > ASP.NET >

正则表达式实例分析

建站学再次给各位奉献一些正则表达式,希望对你们有所帮助。

数学公式正则表达式:
(?'kh'\()*([\-\+]){0,1}[0-9\.]+(?'-kh'\))*([\+\-\*\/]{1}(?'kh'\()*((?<=\()([\-\+]){0,1})?[0-9\.]+(?'-kh'\))*)+(?('kh')(?!))

匹配加减乘除数学公式,如:((1+2)-3*4)

(?'kh'\()*([\-\+]){0,1}[0-9\.]+(?'-kh'\))*([\+\-\*\/]{1}(?'kh'\()*((?<=\()([\-\+]){0,1})?[0-9\.]+(?'-kh'\))*)+(?('kh')(?!))
可以"("开始,压入堆栈统计括号数量,*代表可以出现0次或多次

(?'kh'\()*([\-\+]){0,1}[0-9\.]+(?'-kh'\))*([\+\-\*\/]{1}(?'kh'\()*((?<=\()([\-\+]){0,1})?[0-9\.]+(?'-kh'\))*)+(?('kh')(?!))
可以出现一次正负号

(?'kh'\()*([\-\+]){0,1}[0-9\.]+(?'-kh'\))*([\+\-\*\/]{1}(?'kh'\()*((?<=\()([\-\+]){0,1})?[0-9\.]+(?'-kh'\))*)+(?('kh')(?!))
0-9数字,一个或多个

(?'kh'\()*([\-\+]){0,1}[0-9\.]+(?'-kh'\))*([\+\-\*\/]{1}(?'kh'\()*((?<=\()([\-\+]){0,1})?[0-9\.]+(?'-kh'\))*)+(?('kh')(?!))
可以")"结束,弹出堆栈统计括号数量,*代表可以出现0次或多次

(?'kh'\()*([\-\+]){0,1}[0-9\.]+(?'-kh'\))*([\+\-\*\/]{1}(?'kh'\()*((?<=\()([\-\+]){0,1})?[0-9\.]+(?'-kh'\))*)+(?('kh')(?!))
橙色括号分组说明可以出现一次或多次,如公式1+2 ,这个括号里是匹配 2
红色要求+-*/出现一次

(?'kh'\()*([\-\+]){0,1}[0-9\.]+(?'-kh'\))*([\+\-\*\/]{1}(?'kh'\()*((?<=\()([\-\+]){0,1})?[0-9\.]+(?'-kh'\))*)+(?('kh')(?!))
判断括号开始时,可以有正负号,?代表使用懒惰匹配,即如:1+(((2-3)+4)+5)时,只匹配数字2前面的一个括号一次

(?'kh'\()*([\-\+]){0,1}[0-9\.]+(?'-kh'\))*([\+\-\*\/]{1}(?'kh'\()*((?<=\()([\-\+]){0,1})?[0-9\.]+(?'-kh'\))*)+(?('kh')(?!))
检查括号是否配对