我找到了一個強有力的正則表達式,學分轉到@PhillPafford(PHP RegEx for "Website Name"如果您給予好評我的回答,請給予好評他的第一次!):
/^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)[email protected])?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/
要看到它在行動:
http://rubular.com/r/GOHHrucCdX
UPDATE:
這人會在文本的任何地方看到的姓名:
/(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)[email protected])?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?/
請注意,我刪除了^
在開始和$
末,使之成爲文本內工作:
Rubular來源:
^行首
線$結束
http://rubular.com/r/iEVzfv2U3O
@GandalfStormCrow注意到以下匹配:
Since I was little.My first dog
#^^^
我看到解決這個問題的唯一辦法是更換little.My
與little. My
:
text.gsub(/\w\.[A-Z]/) { |matched_string| matched_string.gsub('.', '. ') }
看到它在行動:
1.9.3p489 :018 > text = "hello my name is robert.My dog"
=> "hello my name is robert.My dog"
1.9.3p489 :019 > text.gsub(/\w\.[A-Z]/) { |matched_string| matched_string.gsub('.', '. ') }
=> "hello my name is robert. My dog"
你最終的目標是什麼? – DickieBoy
@DickieBoy我不想讓人們在我正在驗證的文本框中粘貼網站 –