我有一个将用作配置值的字符串.它就像 1|1|1 或 1|1|2 或 2|1|2 等等.有八种不同的组合.整数是 1 或 2..
I have a string that will be used as a config value.. It is like 1|1|1 or 1|1|2 or 2|1|2 and so on. There are eight different combinations. Integers are either 1 or 2..
问题是我必须在整数之间使用管道.在正则表达式中,管道表示或",但我的字符串中需要它..
The problem is that I have to use pipe between integers. In regex pipe means "or" but I need it in my string..
[1-2][\|][1-2][\|][1-2] 是我想出来的.这是合乎逻辑的还是有更好的选择?
[1-2][\|][1-2][\|][1-2] is what I came up with. Is this logical or are there any better options?
推荐答案我会使用:
([12])\|([12])\|([12])因此您可以同时捕获 1 和 2,并将它们放在一个组中以备将来使用.
So you catch both 1 and 2 and have them in a group for future usage.
减号不是必需的,因为在 1 和 2 之间的 ASCII 中不可能有字符.
The minus sign is not nessesary, because there is no character possible in ASCII between 1 and 2.
更多推荐
带管道的正则表达式
发布评论