2015-08-19 47 views
2

我有以下特定的輸出,我希望從中分離出單詞「industry」(無論哪種情況)和子字符串,直到下一個分隔符典型爲「| 」。我從API獲得$輸出因此,$輸出的內容總是不同,但通用表達式可能類似於:blah blah blah | industry = industry info |等等等等等等。如果行業中存在這個詞,我只想獲得行業=行業信息。有沒有一個通用的正則表達式可以做到這一點?我回來了特定的輸出是:正則表達式來隔離文本正文中的特定單詞,直到分隔符

<?php 

$output = '{{other uses|UBS (disambiguation)}} {{Use dmy dates|date=April 
2015}} {{Infobox company |name = UBS Group AG |logo = [[File:UBS 
Logo.svg|200px|UBS Group AG Logo]] |type = [[Aktiengesellschaft]] 
([[Aktiengesellschaft|AG]]) 
[[Public company]] |traded_as = {{SWX|UBSG}} {{SWX|UBSN}} 
{{nyse|UBS}} |foundation=1854 |predecessor = [[Union Bank of 
Switzerland]] and [[Swiss Bank Corporation]] merged in 1998; 
[[PaineWebber]] merged in 2000 |location = [[Zürich]] 
[[Basel]] |key_people = [[Axel A. Weber]] (Chairman){{br}}[[Sergio 
Ermotti]] (CEO) {{br}} |area_served = Worldwide |industry =[[Banking]], 
[[Financial services]] |products = [[Investment Banking]] 
[[Investment Management]] [[Wealth Management]] [[Private Banking]] 
[[Commercial Bank|Corporate Banking]] 
[[Private Equity]] 
[[Finance and Insurance]] 
[[Retail Banking|Consumer Banking]] 
[[Mortgage loans|Mortgages]] 
[[Credit Cards]] |revenue = {{Increase}} [[Swiss franc|CHF]]28.027 
billion (2014) |operating_income = {{Decrease}} CHF2.461 billion (2014) 
{{cite web|title=UBS Annual Report 
2014|url=http://www.ubs.com/global/en/about_ubs/ 
investor_relations/annualreporting/2014/_jcr_content/par/ 
columncontrol_0/col1/linklist/link.1899571414.file/ 
bGluay9wYXRoPS9jb250ZW50L2RhbS9zdGF0aWMvZ2xvYmFsL2ludmV 
zdG9yX3JlbGF0aW9ucy9hbm51YWwyMDE0L2FubnVhbC1yZXBv 
cnQtZ3JvdXAtMjAxNC1lbi5wZGY=/annual-report-group-2014-  
en.pdf|publisher=UBS.com|accessdate=May 3, 2015}} 
|assets = {{Increase}} CHF1.062 trillion (2014) |equity = {{Increase}} 
CHF54.368 billion (2014) |num_employees = {{Decrease}} 60,155 (2014) 
|caption=We Will Not Rest |homepage = [https://www.ubs.com/ UBS.com] }} 
'''UBS AG''' is a Swiss global [[financial services]] company, 
incorporated in the [[Canton of Zurich]],{{cite web|title=Trade Register: 
UBS AG|url=http://www.moneyhouse.ch/en/u/ubs_ag_CH-270.3.004.646-4.htm}} 
and co-headquartered in [[Zürich]] and [[Basel]].{{cite 
web|url=https://www.ubs.com/global/en/about_ubs/ 
investor_relations/faq/about.html|title=Corporate information - UBS 
Global topics|work=ubs.com|accessdate=March 29, 2015}} The company 
provides [[investment banking]], [[asset management]], and [[wealth 
management]] services for private, corporate, and institutional clients 
worldwide, and for retail clients in Switzerland as well.{{cite 
web|url=https://www.ubs.com/global/en/about_ubs/ 
investor_relations/our_businesses.html|title=Our clients & businesses - 
UBS Global topics|work=ubs.com| 
accessdate=March 29, 2015}} The name ''UBS'' was originally an 
abbreviation for the [[Union Bank of Switzerland]], but it ceased to be a 
representational abbreviation after the bank's merger with [[Swiss Bank 
Corporation]] in 1998. The company traces its origins to 1856, when the 
earliest of its predecessor banks was founded.{{cite web|title=150 years 
of banking tradition|url=https://www.ubs.com/global/en/about_ubs/ 
about_us/history/_jcr_content/rightpar/ 
teaser_0/linklist/link.651908116.file/ 
bGluay9wYXRoPS9jb250ZW50L2RhbS91YnMvZ2xvY 
mFsL2Fib3V0X3Vicy9hYm91dF91cy9oaXN0b3J5X29mX3 
Vicy8xNTBfeWVhcnNfb2ZfYmFua2luZ19FTkcucGRm/ 
150_years_of_banking_ENG.pdf|work=ubs.com| 
accessdate=March 29, 2015}} UBS is the biggest 
bank in Switzerland, operating in more than 50 
countries with about 60,000 employees around the world, as of 2014.{{cite 
web|title=About us: UBS in a few 
words|url=https://www.ubs.com/global/en/ 
about_ubs/about_us/ourprofile.html|work=ubs.com}} It is considered the 
world's largest manager of private wealth assets, with over [[Swiss 
franc|CHF]]2.2 trillion in invested assets,J.P.Morgan Cazenove Europe'; 
?> 

回答

1
[^|]*\bindustry\b[^|]* 

嘗試this.See demo.Use i標誌。

https://regex101.com/r/uF4oY4/79

這將與string|具有industry,直到下一個|後開始。

$re = "/[^|]*\\bindustry\\b[^|]*/i"; 
$str = "{{other uses|UBS (disambiguation)}} {{Use dmy dates|date=April \n2015}} {{Infobox company |name = UBS Group AG |logo = [[File:UBS \nLogo.svg|200px|UBS Group AG Logo]] |type = [[Aktiengesellschaft]] \n([[Aktiengesellschaft|AG]])\n[[Public company]] |traded_as = {{SWX|UBSG}} {{SWX|UBSN}}\n{{nyse|UBS}} |foundation=1854 |predecessor = [[Union Bank of \nSwitzerland]] and [[Swiss Bank Corporation]] merged in 1998; \n[[PaineWebber]] merged in 2000 |location = [[Zürich]]\n[[Basel]] |key_people = [[Axel A. Weber]] (Chairman){{br}}[[Sergio \nErmotti]] (CEO) {{br}} |area_served = Worldwide |industry =[[Banking]], \n[[Financial services]] |products = [[Investment Banking]]\n[[Investment Management]] [[Wealth Management]] [[Private Banking]]\n[[Commercial Bank|Corporate Banking]]\n[[Private Equity]]\n[[Finance and Insurance]]\n[[Retail Banking|Consumer Banking]]\n[[Mortgage loans|Mortgages]]\n[[Credit Cards]] |revenue = {{Increase}} [[Swiss franc|CHF]]28.027 \nbillion (2014) |operating_income = {{Decrease}} CHF2.461 billion (2014) \n{{cite web|title=UBS Annual Report \n2014|url=http://www.ubs.com/global/en/about_ubs/\ninvestor_relations/annualreporting/2014/_jcr_content/par/\ncolumncontrol_0/col1/linklist/link.1899571414.file/\nbGluay9wYXRoPS9jb250ZW50L2RhbS9zdGF0aWMvZ2xvYmFsL2ludmV\nzdG9yX3JlbGF0aW9ucy9hbm51YWwyMDE0L2FubnVhbC1yZXBv\ncnQtZ3JvdXAtMjAxNC1lbi5wZGY=/annual-report-group-2014-  \nen.pdf|publisher=UBS.com|accessdate=May 3, 2015}} \n|assets = {{Increase}} CHF1.062 trillion (2014) |equity = {{Increase}} \nCHF54.368 billion (2014) |num_employees = {{Decrease}} 60,155 (2014) \n|caption=We Will Not Rest |homepage = [https://www.ubs.com/ UBS.com] }} \n'''UBS AG''' is a Swiss global [[financial services]] company, \nincorporated in the [[Canton of Zurich]],{{cite web|title=Trade Register: \nUBS AG|url=http://www.moneyhouse.ch/en/u/ubs_ag_CH-270.3.004.646-4.htm}} \nand co-headquartered in [[Zürich]] and [[Basel]].{{cite \nweb|url=https://www.ubs.com/global/en/about_ubs/\ninvestor_relations/faq/about.html|title=Corporate information - UBS \nGlobal topics|work=ubs.com|accessdate=March 29, 2015}} The company \nprovides [[investment banking]], [[asset management]], and [[wealth \nmanagement]] services for private, corporate, and institutional clients \nworldwide, and for retail clients in Switzerland as well.{{cite \nweb|url=https://www.ubs.com/global/en/about_ubs/\ninvestor_relations/our_businesses.html|title=Our clients & businesses - \nUBS Global topics|work=ubs.com|\naccessdate=March 29, 2015}} The name ''UBS'' was originally an \nabbreviation for the [[Union Bank of Switzerland]], but it ceased to be a \nrepresentational abbreviation after the bank's merger with [[Swiss Bank \nCorporation]] in 1998. The company traces its origins to 1856, when the \nearliest of its predecessor banks was founded.{{cite web|title=150 years \nof banking tradition|url=https://www.ubs.com/global/en/about_ubs/\nabout_us/history/_jcr_content/rightpar/\nteaser_0/linklist/link.651908116.file/\nbGluay9wYXRoPS9jb250ZW50L2RhbS91YnMvZ2xvY\nmFsL2Fib3V0X3Vicy9hYm91dF91cy9oaXN0b3J5X29mX3\nVicy8xNTBfeWVhcnNfb2ZfYmFua2luZ19FTkcucGRm/\n150_years_of_banking_ENG.pdf|work=ubs.com|\naccessdate=March 29, 2015}} UBS is the biggest\nbank in Switzerland, operating in more than 50 \ncountries with about 60,000 employees around the world, as of 2014.{{cite \nweb|title=About us: UBS in a few \nwords|url=https://www.ubs.com/global/en/\nabout_ubs/about_us/ourprofile.html|work=ubs.com}} It is considered the \nworld's largest manager of private wealth assets, with over [[Swiss \nfranc|CHF]]2.2 trillion in invested assets,J.P.Morgan Cazenove Europ"; 

preg_match_all($re, $str, $matches); 
+1

340K步驟當前輸入:你確定一個正則表達式的解決方案是最好的嗎?如果輸入量稍大一點,災難性的回溯即將到來。 –

+0

@stribizhev猜這是OP的一個問題,他已經標記爲正則表達式。如果您有 – vks

+0

,您可以提供另一種解決方案。絕對完美!非常感謝你!! –

1

我不會在像你這樣的大輸入字符串上應用正則表達式。正如你在正則表達式調試器中看到的那樣,vks的正則表達式會產生大約340,000個步驟來最終獲取結果。

我建議首先用|分割字符串,然後找出需要的信息。

$chks = explode("|", $output); 
foreach ($chks as $chk) { 
if (strpos($chk,'industry =') !== false) { 
    echo $chk; 
} 

} 結果:

industry =[[Banking]], 
[[Financial services]] 

IDEONE demo

相關問題