带管道的正则表达式

编程入门 行业动态 更新时间:2024-10-24 16:23:13
本文介绍了带管道的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个将用作配置值的字符串.它就像 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.

更多推荐

带管道的正则表达式

本文发布于:2023-10-27 18:58:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1534258.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:管道   正则表达式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!