canActivate(route: ActivatedRouteSnapshot): Observable<boolean>
{
return concat(this.loadIfRequired(), this.hasBookInStore(route.params['id'])).pipe(skip(1));
}
private loadIfRequired(): Observable<any>
{
return booksState$.pipe(
map(state => state.loaded),
take(1),
tap(loaded=> {
if (!loaded) {
this.store.dispatch(new BooksActions.Load());
}
}));
}
private hasBookInStore(id: string): Obsevable<boolean>
{
return this.booksState$.pipe(
first(state => state.loaded),
map(state => state.list.find(book => book.id === id)),
map((book) => {
if(!!book) return true;
this.router.navigate(['/404']);
return false;
})
);
}
private get booksState$(): Observable<BooksState>
{
return this.store.pipe(
select(state => state.books));
}
我尝试从字符串中提取“ **保险**:不适用”。但是我上面的代码不起作用。我该怎么做?
谢谢!
答案 0 :(得分:0)
我会将内容视为(半结构化的)键值文件格式。
您可以将键值对与正则表达式进行匹配,如下所示:
(\*\*[a-zA-Y ?]+\*\*) : ((?:(?!\*\*).)*)(?= |$)
说明:
(\*\*[a-zA-Y ?]+\*\*)
键:您可能需要调整字符范围:
带有空格的kv分隔符((?:(?!\*\*).)*)
会用一个经过调节的贪婪令牌捕获该值:除文字**
之外的所有内容,其后是(?= |$)
字符串$
的结尾或空格。
(?= | $)import re
regex = r"(\*\*[a-zA-Z ?]+\*\*) : ((?:(?!\*\*).)*)(?= |$)"
test_str = "**Amount** : $25k **Name** : James **Excess** : None Returned \\n **In Suit?** : Y **Venue** : SF **Insurance** : N/A \\n **FTSA** : None listed"
matches = re.finditer(regex, test_str, re.MULTILINE)
for match in matches:
if match.group(1) == "**Insurance**":
print (match.group(2))