【问题标题】:Leading trailing space removal前导尾随空格去除
【发布时间】:2019-12-16 04:26:01
【问题描述】:

我正在从包含以下内容的文本文件中读取内容

    <ID> test data </Id> <Sub_Tab> test data </sub_tab> <form> form data </form>

我的要求是我在 ID、Sub_tab 标记内的任何内容,我想从这些标记内的内容中删除尾随和前导空格,但表单标记内的内容应该保持不变。我的输出应该是:

    <iD>test data</Id> <Sub_Tab>test data</sub_tab> <form> form data </form>

尝试了很多模式,但都没有成功

Regex regex = new Regex(@"/>[ \t]+</");
string newContent = regex.Replace(fileContent, "><");

【问题讨论】:

  • 强制性“如果这是 XML,请使用 XML 解析器”注释。
  • Regex 绝对是错误的工具。使用 XML 解析器和 XPath。在这里,//id/text()|//sub_tab/text()
  • 这看起来像一个 XML 字符串。用正则表达式解析 XML不容易。请改用 XML 解析器。 BTW XML 区分大小写。您不能在 XML 字符串中包含 &lt;ID&gt;&lt;/Id&gt;
  • s = Regex.Replace(Regex.Replace(s, @"\s+(&lt;/(?:ID|Sub_Tab)&gt;)", "$1"), @"(&lt;(?:ID|Sub_Tab)&gt;)\s+", "$1")。甚至Regex.Replace(s, @"\s+(&lt;/(?:ID|Sub_Tab)&gt;)|(&lt;(?:ID|Sub_Tab)&gt;)\s+", "$1$2")
  • @UmeshKumar - XML 也是一个带有标签的文本文件。如果不是太长,您能否发布一个示例文件。如果它遵循 XML 格式,那么解析器绝对是你想要的。

标签: c# regex


【解决方案1】:

这种感觉有点矫枉过正。也许是因为它有点矫枉过正?
无论如何,您可以使用正则表达式轻松完成此操作。但此时,我对正则表达式并不熟悉。
所以,这是我对你的问题的解决方案。来了。

string input = "<ID> test data </Id> <Sub_Tab> test data </sub_tab> <form> form data </form>";

string find = "ƸƷ";
// ƸƷ - If you have these two characters in your input string, then this won't work.
// These characters (ƸƷ) can be replaced with any unique string. However, this function 
// to work, that string should not be contained in the input string 
// or it will mess the replace function. This can be done without using 
// these characters. But it might require more coding. So, I'm going with this.
string str = input;

IList < string > strList = new List < string > ();

// Remove all content inside the form tags
while (true) {
 if ((str.Contains("<form>")) && (str.Contains("</form>"))) {
  int start = str.IndexOf("<form>");
  int end = str.IndexOf("</form>");

  string result = str.Substring(start, end - start + 7); // 7 = "</form>".Length             
  str = str.Replace(result, find);
  strList.Add(result);
 } else {
  break;
 }
}

// Manipulate the data
str = str.Replace(" <", "<").Replace("> ", ">");

// Add the contents inside the form tags
foreach(string val in strList) {
 int place = str.IndexOf(find);
 str = str.Remove(place, find.Length).Insert(place, val);
}

Console.WriteLine("Input String: " + input);
Console.WriteLine("Output String: " + str);  

示例 01

<ID> test data </Id> <Sub_Tab> test data </sub_tab> <form> form data </form> 
<ID>test data</Id><Sub_Tab>test data</sub_tab><form> form data </form>

示例 02

<ID> test data </Id> <Sub_Tab> test data </sub_tab> <form> form data <div> data </div> </form> <br>
<ID>test data</Id><Sub_Tab>test data</sub_tab><form> form data <div> data </div> </form><br>

示例 03

<ID> test data </Id> <form> <span> date </span> </form> <Sub_Tab> test data </sub_tab> <form> form data </form>
<ID>test data</Id><form> <span> date </span> </form><Sub_Tab>test data</sub_tab><form> form data </form>

在线演示:https://rextester.com/FZU31740

【讨论】:

    猜你喜欢
    • 2012-02-09
    • 1970-01-01
    • 2011-08-28
    • 2011-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多