在一行中按顺序获取命名组的跨度

时间:2016-04-25 07:01:06

标签: python regex

我有一个像r'(?P<mark1>)|\s+(?P<mark2>)|(?P<mark3>)\s+'这样的正则表达式(样本正则表达式不是实际的正则表达式),我希望按顺序获取匹配中捕获的组的跨度。

例如:
1. [match.span() for match in re.finditer(regex, string)]按顺序返回跨度但是给出整个匹配的跨度而不仅仅是捕获的组 2. [match.span('mark1') for match in re.finditer(regex, string)]按捕获的组的顺序返回跨度,但将(-1, -1)放入其他命名组。

那么,我可以按照一行中的匹配顺序获取命名组的跨度,就像上面的查询一样简单吗?

我发现了以下方式:
[match.span(name) for match in re.finditer(regex, string) for name, value in match.groupdict().items() if value is not None]

有一个简单的吗?

展示我的场景的一个例子:

import re
s = "asfasdf      32392  ..///?%        aslf    /././/               342"
reg = r'(?P<mark1>[a-z]+)|\s+(?P<mark2>[0-9]+)|(?P<mark3>[./?%]+)\s+'
print([match.span(name) for match in re.finditer(reg, s) for name, value in match.groupdict().items() if value is not None])
print([match.span() for match in re.finditer(reg, s)])
print
print([match.span('mark1') for match in re.finditer(reg, s)])
print([match.span('mark2') for match in re.finditer(reg, s)])
print([match.span('mark3') for match in re.finditer(reg, s)])

输出:

[(0, 7), (13, 18), (20, 27), (35, 39), (43, 49)]
[(0, 7), (7, 18), (20, 35), (35, 39), (43, 64)]

[(0, 7), (-1, -1), (-1, -1), (35, 39), (-1, -1)]
[(-1, -1), (13, 18), (-1, -1), (-1, -1), (-1, -1)]
[(-1, -1), (-1, -1), (20, 27), (-1, -1), (43, 49)]

1 个答案:

答案 0 :(得分:0)

幸运的是,this Stackoverflow answer提供了一个解决方案,您不必多次运行 finditer 。对于当前问题,代码为:

import { Injectable } from "@angular/core";
import { HttpClient } from "@angular/common/http";
import { Observable, Subject, of } from "rxjs";
import { map } from 'rxjs/operators';
import "rxjs/add/operator/map";
import "rxjs/operator/filter";
import { Quote } from "./quote";

@Injectable({
  providedIn: "root"
})
export class QuoteService {
  private baseUrl = "http://localhost:8080/springboot-crud-rest/api/v1/quotes";

  constructor(private http: HttpClient) {}

  getQuote(id: number): Observable<any> {
    return this.http.get(`${this.baseUrl}/${id}`);
  }

  createQuote(quote: Object): Observable<Object> {
    return this.http.post(`${this.baseUrl}`, quote);
  }

  updateQuote(id: number, value: any): Observable<Object> {
    return this.http.put(`${this.baseUrl}/${id}`, value);
  }

  deleteQuote(id: number): Observable<any> {
    return this.http.delete(`${this.baseUrl}/${id}`, { responseType: "text" });
  }

  //getQuotesList(): Observable<any> {
  //  return this.http.get(`${this.baseUrl}`);
  //}

  getQuotesList(): Observable<[string[], string[]]> {
    // replace of(data) to this.http.get(this.baseUrl)
    return this.http.get<Quote[]>(this.baseUrl).pipe(
      map(quotes => {
        return quotes.reduce(
          (acc, curr) => {
            acc[0].push(curr.date.substr(0, 10));
            acc[1].push(curr.open);
            return acc;
          },
          [[], []]
        );
      })
    );
  }
}
从Python 3.6开始,

和dict排序不再是问题