2017-09-20 3 views
1

무엇이든 일치하는 정규식과 구분 기호 사이의 모든 부분 문자열을 반환하는 함수가 필요합니다.PHP - 두 정규식 사이의 부분 문자열 찾기

$str = "{random_one}[SUBSTRING1] blah blah blah {random_two}[SUBSTRING2] blah blah blah{random_one}[SUBSTRING3]"; 

$resultingArray = getSubstrings($str) 

$resultingArray should result in: 
array(
    [0]: "SUBSTRING1", 
    [1]: "SUBSTRING2", 
    [2]: "SUBSTRING3" 
) 

나는 행운이없는 정규식을 망치고있다. 어떤 도움이라도 대단히 감사하겠습니다!

+0

조금 명확하지 않습니다. 괄호 사이에 모든 것이 필요합니까? [this] (https://3v4l.org/GS7YP)를 시도하십시오. – ishegg

+0

@ishegg 예 {anything} [this_is_what_i_need] 다음에 괄호 사이에있는 모든 것 - 확인해 보겠습니다. – hunijkah

+1

아아, 곱슬 곱슬의 중간에 문자열이 있어야한다면 [대신] (https://3v4l.org/)을 시도하십시오. gmh9q). 일치 항목은'$ matches [1]'에 있습니다 – ishegg

답변

2

당신이 정규 표현식 것을 얻을 수 있습니다

/{.+?}\[(.+?)\]/i 

세부 사항은 다음과 같을 것이다 PHP에서

{.+?} # anything between curly brackets, one or more times, ungreedily 
\[  # a bracket, literally 
(.+?) # anything one or more times, ungreedily. This is your capturing group - what you're after 
\]  # close bracket, literally 
i  # flag for case insensitivity 

:

<?php 
$string = "{random_one}[SUBSTRING1] blah [SUBSTRINGX] blah blah {random_two}[SUBSTRING2] blah blah blah{random_one}[SUBSTRING3]"; 
preg_match_all("/{.+?}\[(.+?)\]/i", $string, $matches); 
var_dump($matches[1]); 

Demo

+0

이것은 정확히 내가 필요한 것입니다, 정말 고마워요! – hunijkah

+0

아무 문제 없습니다. 행운을 빕니다! – ishegg

관련 문제