簡體   English   中英

有效文本的正則表達式

[英]Regular Expression for a valid Text

我試圖創建一個驗證的正則表達式!

  1. 如果文本僅包含特殊字符或空格,則應驗證false ...

例如:@#$ ##

  1. 如果結尾處帶有一些文本和特殊字符,則應返回true ...

EX:測試!

  1. 如果以空格或以外的特殊字符開頭。 它應該驗證為false。

例如:@#Test

  1. 如果除空格或逗號以外的特殊字符介於兩者之間。 它應該驗證為假!

例如:te#$ sT

抱歉,對於這個復雜的正則表達式,我的知識不允許我放置任何嘗試的代碼。 因為除了[ab AB !@#$%^&*()]我一無所有

UPDATE

  1. 用Fischermaen的答案進行測試-^(\\ s | \\ w)\\ w * \\ W * $

SDF SDF! :驗證為假

_ __ :驗證為真實(得分以下)

sadf,sdfsdf :驗證為false

空白:驗證為true

嘗試這個:

foundMatch = Regex.IsMatch(subjectString, @"^(?!\W*$)(?=[ .\w]\w*\W*$).*$");

我很想知道你的意思。 您必須學會更好地表達自己。

說明:

"
^            # Assert position at the beginning of the string
(?!          # Assert that it is impossible to match the regex below starting at this position (negative lookahead)
   \W        # Match a single character that is a “non-word character”
      *      # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
   $         # Assert position at the end of the string (or before the line break at the end of the string, if any)
)
(?=          # Assert that the regex below can be matched, starting at this position (positive lookahead)
   [ .\w]    # Match a single character present in the list below
             # One of the characters “ .”
             # A word character (letters, digits, etc.)
   \w        # Match a single character that is a “word character” (letters, digits, etc.)
      *      # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
   \W        # Match a single character that is a “non-word character”
      *      # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
   $         # Assert position at the end of the string (or before the line break at the end of the string, if any)
)
.            # Match any single character that is not a line break character
   *         # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
$            # Assert position at the end of the string (or before the line break at the end of the string, if any)
"

這是我對您的建議:

^(\s|\w)\w*\W*$

說明:

^(\s|\w) the first character has to be a whitespace or a alphanumeric (including underscore)

\w*      followed by any number of alphanumeric (including underscore)

\W*$     at the end of the string any number of any *except* alphanumeric characters are allowed.

據我了解,您只想接受包含一些字母和一些特殊字符的字符串。 所以像這樣:

[(a-zA-Z)+(\!\@\#\$\%\^\&\*\(\))+]

暫無
暫無

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

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