带管道的正则表达式 [英] Reqular Expression with pipe

查看:52
本文介绍了带管道的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个将用作配置值的字符串.它就像 1|1|11|1|22|1|2 等等.有八种不同的组合.整数是 12..

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.

减号不是必需的,因为在 12 之间的 ASCII 中不可能有字符.

The minus sign is not nessesary, because there is no character possible in ASCII between 1 and 2.

这篇关于带管道的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆