{token}的运动部件基于条件
从多行输入中,如果它们内部{}匹配一些数字,我想移动令牌。 示例输入
# (811) (1485) [2756] {29} [555] {15}
# (811) (1476) {20} {15} (1485) [196] [2441]
# (911) (619) {19} (1476) [2765] [2752] {21}
以上行,如果不是{19}或{20}到行末尾,我想移动令牌。
示例输出
# (811) (1485) [2756] [555] {15} {29}
# (811) (1476) {20} (1485) [196] [2441] {15}
# (911) (619) {19} (1476) [2765] [2752] {21}
i可以与preg_match_all(“/\ {\ d+\}/”,$ input,$ matches);
进行preg匹配(“/\ {\ d+\}/”,但是该怎么办?
From a multiline input I want to move tokens if they're inside {} and match some number.
Sample input
# (811) (1485) [2756] {29} [555] {15}
# (811) (1476) {20} {15} (1485) [196] [2441]
# (911) (619) {19} (1476) [2765] [2752] {21}
From the above line, I want to move tokens if it's not {19} or {20} to the end of line.
Sample output
# (811) (1485) [2756] [555] {15} {29}
# (811) (1476) {20} (1485) [196] [2441] {15}
# (911) (619) {19} (1476) [2765] [2752] {21}
I can do a preg match with preg_match_all("/\{\d+\}/", $input, $matches);
but then what to do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以收集数组中每行
{19}
和{20}
的所有匹配项,同时过滤拆分的字符串,然后将它们重新连接在一起。代码示例
输出
关于代码
代码首先在换行符上分割字符串,因为移动部分是每行的。
然后,您可以使用例如explode来分割空格上的行并使用 array_reduce 检查各个部分。
您可以使用包含2个数组的数组来初始化数组reduce
['valid' =>; [], '移动' => []]
在回调函数中,累加器
$acc
已经包含该数组,然后您可以使用$acc 等数组键来填充匹配差异['valid']
模式
{(?!19|20)\d+}
与{
匹配,然后断言它后面不直接跟有 <代码>19} 或20}
如果是这种情况,它将匹配大括号之间的 1 个或多个数字。要获得“单词”之间只有单个空格的结果,您可以合并两个数组,然后在空格上使用 implode。
请参阅 php 演示。
You can gather all the matches for
{19}
and{20}
per line in an array while filtering the splitted string, and then joining them back together.Code example
Output
About the code
Te code first splits the string on newlines, because the moving parts are per line.
Then you can use for example explode to split the line on a space and use array_reduce to inspect the separate parts.
You can initialise array reduce with an array that contains 2 arrays
['valid' => [], 'move' => []]
In the callback function, the accumulator
$acc
then already contains that array, which you can then populate with the difference in matches by using the array key like$acc['valid']
The pattern
{(?!19|20)\d+}
matches{
and then asserts that it is not directly followed by either19}
or20}
If that is the case, it matches 1 or more digits between curly braces.To get a result with just single spaces between the "words" you can merge both arrays, and then use implode on a space.
See a php demo.
该解决方案允许多个令牌标点符号。在此示例中,所有以'{''或'开头的令牌('将移至结尾:
This solution allows for multiple token punctuations. In this example all tokens starting with '{' or '(' will be moved to the end:
您可以使用
preg_replace_callback
来完成此操作:demo
该模式有两个分支:
{19}
或{20}
且尚未位于行尾的每个{token}
。这个分支还包含一个无用捕获组(我把它放在\d+
周围,但你可以把它放在分支中的任何地方,除了在前瞻内部,它也可以是空的)。如果第一个分支成功,则定义捕获组,如果第二个分支成功则不定义。
每次找到匹配项时,其内容都会连接到
$rep
静态变量。但当第二个分支成功时,会返回累积的内容,并将
$rep
重新初始化为空字符串。You can do it using
preg_replace_callback
:demo
The pattern has two branches:
{token}
that are not{19}
or{20}
and that are not already at the end of the line. This branch contains also a useless capture group (I putted it around\d+
but you can put it everywhere in the branch except inside the lookaheads, it can also be empty).If the first branch succeeds, the capture group is defined, if the second branch succeeds it isn't.
Each time a match is found, its content is concatenated to the
$rep
static variable.But when the second branch succeeds, this accumulated content is returned and
$rep
is reinitialized to the empty string.