【问题标题】:Validate 3 chunks of strings验证 3 块字符串
【发布时间】:2023-03-24 07:05:01
【问题描述】:

我真的很难完成这项工作。

我有一个string mess,我需要将其分成三块。我已经正确完成了这项工作,但现在我陷入了如何验证最后两个带有特殊字符的字符串的前两个块(i_clob1i_clob2"<"">""</" - 因为当我剪切了这三个块,我的存储过程附加了三个字符串,当它被剪切到这些字符中时看起来很糟糕。

如果第一个 (i_clob1) 或第二个 (i_clob2) 块最后包含字符,我想知道如何扩展字符串,以便我没有最后的字符或者修剪它?在不丢失字符的情况下。然后字符将在下一个字符串中。最后一个块 (i_clob3) 不需要验证,因为它总是有最后一位文本。

我真的希望有人能解答我的谜语:-)

我的快乐方案是在一些随机文本而不是那些字符中剪切字符串。

我当前的代码:

public void Enqueue(string queueName, string mess)
        {
            if (mess.Length >= 3 && mess.Length <= 32000 * 3)
            {
                int lastStart = 2 * mess.Length / 3;
                int lastLength = mess.Length - lastStart;

                string i_clob1 = mess.Substring(0, (mess.Length / 3));
                string i_clob2 = mess.Substring(mess.Length / 3, mess.Length / 3);
                string i_clob3 = mess.Substring(lastStart, lastLength);


            OracleCommand cmd = null;
            try
            {
                cmd = new OracleCommand("", m_Connection)
                    {
                        CommandText = m_InSpName,
                        CommandType = CommandType.StoredProcedure
                    };


                //add Aq queue name 
                OracleParameter qName = new OracleParameter("qname", OracleType.VarChar)
                    {
                        Direction = ParameterDirection.Input,
                        Value = queueName
                    };

                //add message to enqueue
                OracleParameter message1 = new OracleParameter("i_clob1", OracleType.Clob)
                    {
                        Direction = ParameterDirection.Input
                    };
                OracleParameter message2 = new OracleParameter("i_clob2", OracleType.Clob)
                {
                    Direction = ParameterDirection.Input
                };
                OracleParameter message3 = new OracleParameter("i_clob3", OracleType.Clob)
                {
                    Direction = ParameterDirection.Input
                };

                i_clob1 = i_clob1.Replace("<?xml version=\"1.0\" encoding=\"utf-16\"?>", "");
                message1.Value = i_clob1;
                message2.Value = i_clob2;
                message3.Value = i_clob3;

                cmd.Parameters.Add(qName);
                cmd.Parameters.Add(message1);
                cmd.Parameters.Add(message2);
                cmd.Parameters.Add(message3);

                cmd.ExecuteNonQuery();

            }
            catch (Exception ex)
            {
                //rethrow exception and make sure we clean up i.e. execute finally below
                throw new Exception("An error has occurred trying to deliver to the queue", ex);
            }

            finally
            {
                if (cmd != null)
                {
                    cmd.Dispose();
                }
            }

        }
    }

这是我输入的一个示例。通常总共大约 30.000 个字符。

<item>
    <item_number>1231</item_number>
    <item_title>Lorem ipsum dolor sit</item_title>
    <item_pbl_code>Lorem ipsum dolor sit</item_pbl_code>
    <item_dep_code>Lorem ipsum dolor sit</item_dep_code>
    <item_off_code>Lorem ipsum dolor sit</item_off_code>
    <item_digitized_timestamp>2013-11-04 09:07:56</item_digitized_timestamp>
    <item_source_url>Loremadsa/adad1231/12312</item_source_url>
    <item_cat_code>Lorem ipsum dolor sit</item_cat_code>
    <item_ars_code>Lorem ipsum dolor sit</item_ars_code>
    <item_ric_code>Lorem ipsum dolor sit</item_ric_code>
    <item_rle_code>Lorem ipsum dolor sit</item_rle_code>
    <item_code>Lorem ipsum dolor sit</item_code>
    <subjects>
        <sub_keyword />
    </subjects>
    <item_description1>A lot of text goes here</item_description1>
    <item_description2>A lot of text goes here</item_description2>
    <item_description3>A lot of text goes here</item_description3>
</item>

描述字段通常约为 10 - 20.000 个字符

我的输出将是新的解决方案:

第一块

 <item>
        <item_number>1231</item_number>
        <item_title>Lorem ipsum dolor sit</item_title>
        <item_pbl_code>Lorem ipsum dolor sit</item_pbl_code>
        <item_dep_code>Lorem ipsum dolor sit</item_dep_code>
        <item_off_code>Lorem ipsum dolor sit</item_off_code>
        <item_digitized_timestamp>2013-11-04 09:07:56</item_digitized_timestamp>
        <item_source_url>Loremadsa/adad

第二块

1231/12312</item_source_url>
    <item_cat_code>Lorem ipsum dolor sit</item_cat_code>
    <item_ars_code>Lorem ipsum dolor sit</item_ars_code>
    <item_ric_code>Lorem ipsum dolor sit</item_ric_code>
    <item_rle_code>Lorem ipsum dolor sit</item_rle_code>
    <item_code>Lorem ipsum dolor

第三块

      sit</item_code>
    <subjects>
        <sub_keyword />
    </subjects>
    <item_description1>A lot of text goes here</item_description1>
    <item_description2>A lot of text goes here</item_description2>
    <item_description3>A lot of text goes here</item_description3>
</item>

【问题讨论】:

  • 如果您可以发布输入和预期输出的示例,这可能会有所帮助。
  • 我会选择 XDocument 或其他一些内置功能来解析 xml,而不是将其视为文本。
  • 您的解释、您的知识、您的努力等不在此处所期望的范围内(或多或少在定制软件开发公司对其客户的期望范围内:未明确定义的列表要求)。正如你所看到的,我已经发布了一个答案(尽管,当你在上一个问题中没有太多交流时,我不太喜欢你删除的那个),因为就我提供了以前的算法......但是,请将此视为完全不同寻常的事情。希望你在不久的将来能按预期使用 SO。
  • @varocarbas 很遗憾听到您没有按照您希望的方式看待我的问题,但我正在尽我所能解释这种情况。我有一段时间试图解决这个问题,但没有很好的尝试,也没有很好的代码示例。因此,我没有上一篇文章中的谜语示例。无论如何,我感谢您的回答和您的努力。
  • 您不必感到抱歉。如果您认为我是对的,请在这方面加倍努力(我总是说些有帮助的话……您在 SO 方面没有太多经验,并认为您需要对大多数人在这里思考)。

标签: c# regex string substring


【解决方案1】:

根据您的描述(以及您之前的问题),我了解到您希望通过同时考虑块大小和块内容来执行除法;不能同时考虑这两个限制,并且在任何情况下,您都必须设置首选项(例如,如果给定的块包含必须“移动”到下一个块的字符,但下一个块已经具有最大值大小,应该怎么做?)。我了解此偏好的定义如下:

  • 您根据大小 (30000) 划分块,但允许 总是足够大的“冗余位”(允许的最大大小为 32000)。
  • 按内容的更正(如下代码所示)是在上述按大小划分之后执行的;因此可以安全地假设不会有大小限制(可以盲目地应用修正,在最坏的情况下,总会有 2000 的冗余长度)。

执行上述更正的代码:

string i_clob1 = "anything1 </";
string i_clob2 = "anything2 </";
string i_clob3 = "anything3 </";

string allTogether = i_clob1 + i_clob2 + i_clob3;
int start2 = i_clob1.Length;
int length2 = i_clob2.Length;
int start3 = start2 + length2;

string[] bitsToAvoid = new string[] { "</", "<", ">"};
string i_clob1_out = i_clob1;
foreach (string bit in bitsToAvoid)
{
    if (i_clob1_out.Substring(i_clob1_out.Length - bit.Length) == bit)
    {
        start2 = start2 - bit.Length;
        length2 = length2 + bit.Length;
        i_clob1_out = allTogether.Substring(0, start2);
        break; //Just one wrong bit is assumed to be present
    }
}
string i_clob2_out = allTogether.Substring(start2, length2);
foreach (string bit in bitsToAvoid)
{
    if (i_clob2_out.Substring(i_clob2_out.Length - bit.Length) == bit)
    {
        start3 = start3 - bit.Length;
        i_clob2_out = allTogether.Substring(start2, start3 - start2);
        break; //Just one wrong bit is assumed to be present
    }
}
string i_clob3_out = allTogether.Substring(start3);


i_clob1 = i_clob1_out; //"anything1 "
i_clob2 = i_clob2_out; //"</anything2" 
i_clob3 = i_clob3_out; //"</anything3 </" 

注意:此答案相对适应性强(bitsToAvoid 可以根据需要使用尽可能多的元素进行更新),但在一定范围内:它是在阅读问题的原始描述后创建的,其中包含入围数量的字符提到。如果打算以更复杂的方式定义“要避免的位”(例如,确保给定的关闭节点标签是否存在),则该算法应仅作为起点,并且必须得到显着改善(很可能,通过考虑一些 XML 分析)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-14
    • 1970-01-01
    • 2011-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-20
    相关资源
    最近更新 更多