以规则的间隔在字符串中插入分隔符
我在 php 中有以下字符串:
$string = 'FEDCBA9876543210';
该字符串可以有 2 个或更多(我的意思是更多)十六进制字符 我想将字符串按 2 进行分组,例如:
$output_string = 'FE:DC:BA:98:76:54:32:10';
我想使用正则表达式,我想我看到了一种类似于“递归正则表达式”的方法,但我不记得了。
任何帮助表示赞赏:)
I have the following string in php:
$string = 'FEDCBA9876543210';
The string can be have 2 or more (I mean more) hexadecimal characters
I wanted to group string by 2 like :
$output_string = 'FE:DC:BA:98:76:54:32:10';
I wanted to use regex for that, I think I saw a way to do like "recursive regex" but I can't remember it.
Any help appreciated :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果不需要检查内容,则正则表达式没有用处。
试试这个
您可能需要删除最后一个“:”。
或者这个:
资源:
同一主题:
If you don't need to check the content, there is no use for regex.
Try this
You might need to remove the last ":".
Or this :
Resources :
On the same topic :
听起来你想要一个像这样的正则表达式:
Which, in PHP is...
请注意上面的代码当前未经测试。
Sounds like you want a regex like this:
Which, in PHP is...
Please note the above code is currently untested.
您可以确保有两个或更多十六进制字符执行此操作:
不需要递归正则表达式。顺便说一下,递归正则表达式是一个矛盾的术语。根据定义,作为常规语言(正则表达式解析)不能递归。
如果您还想将字符与中间的冒号成对分组,暂时忽略两个十六进制字符,请使用:
现在,如果您想添加需要两个十六进制字符的条件,请使用 正向前瞻:
为了解释它是如何工作的,它所做的第一件事就是检查(使用正向前瞻,即
(?=...)
您有零个或多个数字或冒号,后跟一个十六进制字母,后跟零个或多个数字或冒号,然后是一个字母,这将确保表达式中有两个十六进制字母。 正向先行之后是确保字符串是十六进制数字对的原始表达式。
You can make sure there are two or more hex characters doing this:
No need for a recursive regex. By the way, recursive regex is a contradiction in terms. As a regular language (which a regex parses) can't be recursive, by definition.
If you want to also group the characters in pairs with colons in between, ignoring the two hex characters for a second, use:
Now if you want to add the condition requiring tow hex characters, use a positive lookahead:
To explain how this works, the first thing it does it that it checks (with a positive lookahead ie
(?=...)
that you have zero or more digits or colons followed by a hex letter followed by zero or more digits or colons and then a letter. This will ensure there will be two hex letters in the expression.After the positive lookahead is the original expression that makes sure the string is pairs of hex digits.
递归正则表达式通常是不可能的。您可以在前一个正则表达式的结果上递归地使用正则表达式,但大多数正则表达式语法不允许递归。这就是正则表达式几乎总是不足以解析 HTML 等内容的主要原因。无论如何,您所需要的不需要任何类型的递归性。
简而言之,您想要的就是多次匹配一个组。这非常简单:
这将填充所有出现的两个十六进制数字(以不区分大小写的方式)。要替换它们,请使用 preg_replace:
末尾可能会出现过多的 ':',您可以使用
substr
将其删除:Recursive regular expressions are usually not possible. You may use a regular expression recursively on the results of a previous regular expression, but most regular expression grammars will not allow recursivity. This is the main reason why regular expressions are almost always inadequate for parsing stuff like HTML. Anyways, what you need doesn't need any kind of recursivity.
What you want, simply, is to match a group multiple times. This is quite simple:
This will fill
$matches
will all occurrences of two hexadecimal digits (in a case-insensitive way). To replace them, use preg_replace:There will probably be one ':' too much at the end, you can strip it with
substr
:虽然使用 rtrim(chunk_split($string, 2, ':'), ':') 并不是可怕的做法,但我更喜欢使用直接技术,以避免在进行修改后“清理”。
代码:(演示)
输出:
不要被正则表达式吓倒。该模式表示:
当我说“重新启动全字符串匹配”时,我的意思是“忘记以前匹配的字符并从此时开始匹配”。由于
\K
之后没有其他匹配的字符,因此该模式有效地提供了应插入冒号的零宽度位置。这样,替换时就不会丢失原来的字符。While it is not horrible practice to use
rtrim(chunk_split($string, 2, ':'), ':')
, I prefer to use direct techniques that avoid "mopping up" after making modifications.Code: (Demo)
Output:
Don't be intimidated by the regex. The pattern says:
When I say "restart the fullstring match" I mean "forget the previously matched characters and start matching from this point forward". Because there are no additional characters matched after
\K
, the pattern effectively delivers the zero-width position where the colon should be inserted. In this way, no original characters are lost in the replacement.