我正在嘗試創建一個正則運算式來查找連接的字串或帶有隨機大寫單詞的字串。我需要找到諸如“EmployeeID”、“messageIndex”、“JOBname”、“KeyRange”、“Type21”等內容。我很好地使用“^[A- Za-z0-9] (?:_[A-Za-z0-9] ) $",但是如何找到沒有分隔符的字串?我只是不斷地找到所有的詞。謝謝!
uj5u.com熱心網友回復:
請查看這是否適用于您的用例:
\b((([A-Z0-9] [a-z0-9]*) )|(([a-z0-9] [A-Z0-9]*)) )\b
我在以下字串上對此進行了測驗,并且能夠匹配每個字串:“EmployeeID”、“messageIndex”、“JOBname”、“KeyRange”、“Type21”
這會檢查至少存在一次并且可能重復也可能不重復的大寫和小寫字母(以及數字)的交替。\b 是建立單詞邊界。如果您不發布您嘗試決議的確切字串(或它的變體,如果有敏感資訊),則很難在不知道字串結構的情況下確定適合您情況的運算式。
uj5u.com熱心網友回復:
用
^[A-Za-z] (?:[A-Z0-9] [A-Za-z0-9]*) $
請參閱正則運算式證明。簡而言之:開始匹配至少一個字母,然后需要一個大寫字母或數字,然后匹配任何字母和數字。
解釋
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
[A-Za-z] any character of: 'A' to 'Z', 'a' to 'z'
(1 or more times (matching the most amount
possible))
--------------------------------------------------------------------------------
(?: group, but do not capture (1 or more times
(matching the most amount possible)):
--------------------------------------------------------------------------------
[A-Z0-9] any character of: 'A' to 'Z', '0' to '9'
(1 or more times (matching the most
amount possible))
--------------------------------------------------------------------------------
[A-Za-z0-9]* any character of: 'A' to 'Z', 'a' to
'z', '0' to '9' (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
) end of grouping
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/321990.html
下一篇:比較JAVA中字串內的數值
