我需要一个允许将{{[1][value|other]}}
转换为
的php正则表达式Hello {{[1][miss|mr]}} {{[2][Maria|Jon]}}
我尝试了很多正则表达式,但没有结果,它仅在第一次出现时起作用:
我的文字:Hello
$pattern = '/{{\[(\d+)\].+}}/';
$matches = [];
preg_match($pattern, $content, $matches);
{{2}}
我想要的结果:Array
(
[0] => {{[1][miss|mr]}} {{[2][Maria|Jon]}}
[1] => 1
)
import { combineReducers } from 'redux-immutable';
import { reducer as formReducer } from 'redux-form';
import appReducer from './appReducer';
import errorReducer from './errorReducer';
import loginReducer from './loginReducer';
import modalReducer from './modalReducer';
import changeRequestReducer from './changeRequestReducer';
import releaseReducer from './releaseReducer';
import releaseReviewReducer from './releaseReviewReducer';
import logoutReducer from './logoutReducer';
import customAvatarReducer from './customAvatarReducer';
export default combineReducers({
appReducer,
errorReducer,
loginReducer,
changeRequestReducer,
releaseReducer,
modalReducer,
releaseReviewReducer,
form: formReducer,
logoutReducer,
customAvatarReducer,
});
给我:
const rootReducer = (state, action) => {
if (action.type === USER_LOGOUT) {
const state = undefined;
}
return reducers(state, action);
};
const reducers = combineReducers({
appReducer,
errorReducer,
loginReducer,
changeRequestReducer,
releaseReducer,
modalReducer,
releaseReviewReducer,
form: formReducer,
logoutReducer,
customAvatarReducer,
});
但是我还需要“ 2”
答案 0 :(得分:2)
这很简单,只需匹配/使用要删除的字符并捕获要保留的字符即可。
正则表达式演示:https://regex101.com/r/GdIsZX/1/
$text = 'Hello {{[1][miss|mr]}} {{[2][Maria|Jon]}}';
echo preg_replace('~\[(\d+)]\[[^]]*]~', '$1', $text);
输出:
Hello \K
{{2}}
以上模式并非旨在验证子字符串。重点是有效的广义匹配。如果您需要更严格/准确的模式,则需要通过更好地代表您的挑战的输入来改进问题。
p.s。如果要匹配花括号但不删除它们,则可以使用~{{\K\[(\d+)]\[[^]]*](?=}})~
和前瞻。
angular