這可能嗎?兩個字符匹配
從這個字符串:
string = "lsdfh892pr23hr4342pr3j4r\n\nwww.foobar.com•\nyahoogooglebing"
我想提取"www.foobar.com"
。
而且,從這個:
string = "lsdfh892pr23hr4342pr3j4r\n\[email protected]•\nyahoogooglebing"
我想提取"[email protected]"
。
這可能嗎?兩個字符匹配
從這個字符串:
string = "lsdfh892pr23hr4342pr3j4r\n\nwww.foobar.com•\nyahoogooglebing"
我想提取"www.foobar.com"
。
而且,從這個:
string = "lsdfh892pr23hr4342pr3j4r\n\[email protected]•\nyahoogooglebing"
我想提取"[email protected]"
。
我不會用正則表達式麻煩:
string = "lsdfh892pr23hr4342pr3j4r\n\nwww.foobar.com•\nyahoogooglebing"
string.split("\n")[2][0..-2]
=> "www.foobar.com"
string = "lsdfh892pr23hr4342pr3j4r\n\[email protected]•\nyahoogooglebing"
string.split("\n")[2][0..-2]
=> "[email protected]"
split是一種簡單的正則表達式,在這裏,它不會簡化代碼,使提取更加靈活(您不知道每次電子郵件或域是否位於第三行),並且不會提高性能。 – Gael 2013-03-26 02:16:06
是的,'split'強制字符串'「\ n」'爲正則表達式,但是,它減輕了OP需要維護任何更復雜的模式。由OP決定該字符串是否始終在第三行上有站點名稱。在這種情況下,它與給出的數據樣本一致,並且假設總是有一個「子彈」或兩個程序「\ n」同樣安全。 – 2013-03-26 02:33:32
好吧,正則表達式可能很複雜。然而,我認爲我更喜歡具有良好語義的代碼。而'split'並不是專門提取內容的。 – Gael 2013-03-26 03:28:05
你有什麼要提取?匹配域模式的字符串或新行之間的字符串? – Gael 2013-03-26 01:36:59
字符串\ n \ n和... ... – sambehera 2013-03-26 01:39:34