简体   繁体   中英

Does order of characters in character class matter in regular expressions

I was using character class in regular expression to split a string. I had an assumption that order of characters in the character class does not matter.

Following are the two patterns I was using to split the string "123.3+23+23.3" .

[+/*-]

[+*-/]

For the first pattern I got the output as:

123.3
23
23.3

For the second pattern I got the output as :

123
3
23
23
3

I've no idea, why I am getting the different outputs. Please help me out.

Within a character class, - is a range operator (as in [af] is the same as [abcdef] ). So if you want to include an actual - in your range, it must be the first or last character.

Therefore, your first example will match + / * - , while your second will match + / * - , . .

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM