如何在Sublime Text中的xml标记内删除特定的空格? [英] How do I remove specific whitespace inside xml tag in Sublime Text?

查看:129
本文介绍了如何在Sublime Text中的xml标记内删除特定的空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件,其中包含一些遵循特定模式的XML标记(名称和道具为占位符)

I have a file with some xml tags that follow specific patterns (Name and Props are placeholders)

<Name id="mod:Name"/>
<Prop1 Name id="mod:object.Prop1 Name"/>
<Prop1 Prop2 Name id="mod:object.Prop1 Prop2 Name"/>
<Prop1 Prop2 Prop3 Name id="mod:object.Prop1 Prop2 Prop3 Name"/>

我正在寻找正则表达式以删除"id = ..."之前部分的空白

I am looking for regex to remove whitespace from portion before the "id=..."

<Name id="mod:Name"/>
<Prop1Name id="mod:object.Prop1 Name"/>
<Prop1Prop2Name id="mod:object.Prop1 Prop2 Name"/>
<Prop1Prop2Prop3Name id="mod:object.Prop1 Prop2 Prop3 Name"/>

我已经看到了(\S+)\s(?=\S+\s+)的示例,其中的替换只是\1,但是它删除了除最后一个空格以外的所有空格,并且在id =

I have seen the (\S+)\s(?=\S+\s+) example with the substitution being just \1 but that removes all the spaces except the last one and doesn't leave a space before the id=

<Name id="mod:Name"/>
<Prop1Name id="mod:object.Prop1 Name"/>
<Prop1Prop2Name id="mod:object.Prop1Prop2 Name"/>
<Prop1Prop2Prop3Name id="mod:object.Prop1Prop2Prop3 Name"/>

我尝试过类似的事情

但这给了我灾难性的回溯

But that gave me catastrophic backtracking

不确定是否有帮助,但是Sublime使用Boost regex

Not sure if it helps but Sublime uses Boost regex

第一个关于The Stack的问题,欢迎对此问题进行任何改进

First question on The Stack so any improvements on question would be welcome

谢谢

这似乎可行

^(?|((\S+))\s|((\S+)\s(\S+))\s|((\S+)\s(\S+)\s(\S+)\s))(id=.*)

替换为$2$3$4 $5

感谢您的建议

推荐答案

正确的正则表达式,用于删除id属性之前的所有空格

A correct regex for removing all whitespaces before the id attribute will be

(?:<\w+|(?!^)\G)\K\s+(\w+)(?=[^<>]*\bid=")

替换为$1.请参见 regex演示.

regex使用\G运算符(如果使用(?!^) lookahead进行限制,则匹配上一次成功匹配之后的位置)和\K运算符,该运算符将舍弃到目前为止与该模式匹配的文本.

The regex uses the \G operator (matches the location after the last successful match if restricted with (?!^) lookahead) and the \K operator that discards the text that was matched by the pattern so far.

故障:

  • (?:<\w+|(?!^)\G)\K-匹配<,后跟1+个字母数字或下划线字符,或最后一次成功匹配的末尾,并忽略找到的文本
  • \s+-匹配1+个空格符号
  • (\w+)-将一个或多个字母数字或下划线字符匹配并捕获到第1组中(我们稍后将使用$1后向引用来在结果中恢复此消耗的文本)
  • (?=[^<>]*\bid=")-仅在匹配空格后跟字母数字,直到找到整个单词id=(\b是单词边界)但在标记内(由于[^<>]*匹配零个或多个除<>以外的字符).
  • (?:<\w+|(?!^)\G)\K - match < followed with 1+ alphanumeric or underscore characters or the end of the last successful match and omit the text found
  • \s+ - match 1+ whitespace symbols
  • (\w+) - match and capture into Group 1 one or more alphanumeric or underscore characters (we'll later use a $1 backreference to restore this consumed text in the result)
  • (?=[^<>]*\bid=") - only go on matching spaces followed with alphanumerics until it finds id= as a whole word (\b is a word boundary) but inside the tag (due to the [^<>]* matching zero or more characters other than < and >).

一种更快的替代方法(用空字符串替换):

A faster alternative (to replace with empty string):

(?:<|(?!^)\G)\w+\K\s+(?!id=)

此正则表达式匹配<或最后一次成功匹配的末尾,然后是一个或多个单词字符,然后\K将从匹配中忽略整个文本,并且仅匹配1个或多个空格(如果由于最后的负向(?!id=))后面没有紧跟id=,因此它们将被删除.

This regex matches the < or the end of the last successful match, then one or more word characters, then \K will omit the whole text from the match, and only 1 or more whitespaces will be matched (if not followed with id= due to the negative lookahead (?!id=)) in the end - and they will be removed.

这篇关于如何在Sublime Text中的xml标记内删除特定的空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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