簡體   English   中英

檢查字符串是否僅包含某個單詞?

[英]Check if a string contains a certain word only?

我試圖找到一種方法來檢查字符串是否僅包含某個單詞,或者某個單詞和日期。 假設該字符串可以包含多個單詞(如一個簡短的句子),以及一個或多個日期(以連字符分隔)。 例如,如果某個單詞是“ On”或“ on”,則...

string | result
"On" | true
"On 4/31" | true
"on 4/31/2018" | true
"4/13 On" | true
"4/31-5/4 on" | true
"4/31 - 5/4 on" | true
"Turn on" | false
"Turn on 5/4" | false
"On site" | false
"On 4/15 abc" | false
<Any other string that does not contain "on" at all> | false

如您所見,要使字符串為true ,字符串中的某處必須“開”,並且還可以具有一個或多個日期表達式(帶連字符)。 但是,一旦它包含另一個字母單詞(例如“ turn”或“ abc”或其他任何東西),則需要返回false “開”或“開”需要出現一次,並且必須是整個字符串中的唯一單詞。 順序無關緊要,因此“ on”或“ On”可以出現在字符串的開頭或結尾”

有什么辦法可以檢查嗎? 我首先想到了str.Contains("On")但是當它需要返回false時,它對於“ Turn on”也返回true。 str.StartsWith("On")str.EndsWith("On")也不起作用。

順便說一句,“開”和“開”都可以,所以我要在檢查之前ToLower字符串,以便“開”和“開”都將得到相同的結果。

我希望我的問題有道理,並且有人可以給我一些建議,以幫助我實現這一目標嗎?

編輯:這是我編寫的一些代碼,盡管由於我仍在努力,所以它並不完整。

//str is the string to be examined
private bool IsValidString(string str)
{
    //Lowercase the original string 
    string lower = str.ToLower();

    //Get the number of occurrences of the substring "on"
    int occurrence = System.Text.RegularExpressions.Regex.Matches(lower, "on").Count;

    //If the occurrence count is 0, or more than 1, return false
    if (occurrence == 0 || occurrence > 1)
            return false;

    //Split the string by ' '
    string[] strArr = lower.Split(' ');

    for (int i = 0; i <= strArr.Length; i++)
    {
        //struggling in here... need to check if any other word
        //is included in the array. Still date expression is allowed.
    }
}

如果我正確理解了您的問題,那么您想檢查字符串是包含“ On”還是“ on”,但是您可以將其降低,以便僅將其“ on”?

您可以將字符串拆分為空格,然后檢查是否匹配“ on”(我猜)。 這只是一個簡單的例子。 您可能可以通過更好的方式來做到這一點,但這只是PoC

string example = "On 4/15 abc";
string[] words = example.Split(' ');
bool match = false;
foreach(string word in words)
{
    if (word.Any(x => char.IsLetter(x))) {
        if (word.ToLower() == "on") 
        {
            match = true;
        } 
        else 
        { 
            match = false; break;
        }
    }
}

正如我在評論中已經描述的那樣:

private bool IsValidString(string str)
{
    //Lowercase the original string 
    string lower = str.ToLower();
    //split by space. 
    List<string> parts = lower.Split(' ').ToList();

    //Then check if it contains "on" 
    if (parts.Contains("on"))
    {
        // get rid of the disturbing influence of "on"
        parts.Remove("on");
        //and furthermore check if the rest contains at least on further letter. This allows for numbers to appear
        if (parts.Any(x => x.ToCharArray().Any(y=> char.IsLetter(y))))
        {
            return false;
        }
        // CHECK here for the date format
        return true; // here only "on2 exists in the string as a word
    }
    else // if no "on" then it is false anyway
    {
        return false;
    }   
}

您可以使用Char.IsLetter來檢查字符串的其余部分是否包含任何字母

編輯:此代碼將解析您給出的所有示例。 我需要在注釋中注明的日期格式檢查

在這種情況下,您必須使用語法分析器來告訴您單詞在句子中的作用。 因此,如果它是介詞,我們認為它是假的,依此類推。 是一個實現英語語法分析器的庫。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM