Я хочу извлечь строку после = по rexgs в Java - PullRequest
0 голосов
/ 24 апреля 2020

У меня есть файл, который содержит

{{cite web |url=http://www.canada.lu |title=Gouvernement du Canada -
 Government of Canada |publisher= |accessdate=20 February 2015}}


{{cite book
|url=https://books.google.com/?id=U3L6H8eOIb0C&pg=PA117&dq=Estonia+and+Luxembourg#PPA118,M1
|title=The Radical Right in Interwar Estonia |publisher=
|accessdate=20 February 2015 |isbn=9780312225988 |last1=Kasekamp
|first1=Andres

Я написал код в Java, используя regular expression для извлечения между {{cite web ....}}

Pattern Pattern3 = Pattern.compile("\\{cite book([^}]+)\\}", Pattern.DOTALL);

Matcher matcher3 = Pattern3.matcher(t);

Pattern Pattern4 = Pattern.compile("\\{cite web([^}]+)\\}", Pattern.DOTALL);

Matcher matcher4 = Pattern4.matcher (t);


Это дает мне все вещи между {{cite web .... }} и {{cite book ...}}

, но я хочу извлечь все строки после = и до |

как title=The Radical Right in Interwar Estonia

Я хочу вывод вот так:


WEB:

Title = Gouvernement du Canada - Government of Canada
URL = http://www.canada.lu
Access Date =20 February 2015

BOOK:
Title =  The Radical Right in Interwar Estonia
URL = https://books.google.com/?id=U3L6H8eOIb0C&pg=PA117&dq=Estonia+and+Luxembourg#PPA118,M1
publisher = NULL

Спасибо

Мой файл , который я хочу извлечь из .

Ответы [ 2 ]

0 голосов
/ 30 апреля 2020

Кажется, что нам нужно обнаружить cite записей и затем проанализировать их содержимое в поля, как предложено здесь

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class ParseWikiData {

    static final int PREFIX_GROUP = 1;
    static final int FIELD_NAME_GROUP = 3;
    static final int FIELD_VALUE_GROUP = 4;

    public static void main(String[] args) throws IOException {
        String entryRegex = "(?i:\\{\\{\\s*cite\\s+(book|journal)[^}]*\\}\\})";
        Pattern entryPattern = Pattern.compile(entryRegex);

        String fieldRegex = "(?i:(?<=^|\\|)(\\{\\{Cite\\s+(book|journal)\\s*)|([^{|}\\=]+)\\s*\\=\\s*([^|}]*))";
        Pattern fieldPattern = Pattern.compile(fieldRegex);

        String txt = readFile("wikisample.xml");

        Matcher entryMatcher = entryPattern.matcher(txt);
        int count = 0;
        while (entryMatcher.find()) {
            System.out.println("Entry #" + ++count + ": prefix: " + entryMatcher.group(PREFIX_GROUP).trim());
            String content = entryMatcher.group(0).trim();
            Matcher fieldMatcher = fieldPattern.matcher(content);

            while (fieldMatcher.find()) {
                if (fieldMatcher.group(FIELD_NAME_GROUP) != null) {
                    String key = fieldMatcher.group(FIELD_NAME_GROUP).trim();
                    String value = fieldMatcher.group(FIELD_VALUE_GROUP).trim();
                    System.out.println("\t" + key + " = " + value);
                }
            }
        }
    }

    private static String readFile(String fileName) throws IOException {
        return Files.lines(Path.of(fileName)).collect(Collectors.joining());
    }
}

Для примера файла вики выдается следующий вывод (обнаруживает как книги, так и журналы):

Entry #1: prefix: book
    url = https://books.google.com/?id=U3L6H8eOIb0C&amp;pg=PA117&amp;dq=Estonia+and+Luxembourg#PPA118,M1
    title = The Radical Right in Interwar Estonia
    publisher = 
    accessdate = 20 February 2015
    isbn = 9780312225988
    last1 = Kasekamp
    first1 = Andres
    date = 2000-06-03
Entry #2: prefix: book
    title = Le Grand-Duché de Luxembourg et la construction européenne
    last = Fally
    first = V.
    publisher = 
    year = 1992
    isbn = 
    location = Luxembourg City
    pages = 
Entry #3: prefix: book
    title = Le Luxembourg face à la construction européenne
    last = Fally
    first = V.
    publisher = 
    year = 1996
    isbn = 
    location = Luxembourg City
    pages = 
Entry #4: prefix: book
    title = Small States in Europe: Challenges and Opportunities
    last = Frentz
    first = Jean-Marie
    publisher = Ashgate
    year = 2010
    isbn = 978-0-7546-7782-6
    location = 
    pages = 
    editor-last = Steinmetz
    editor-first = Robert
    editor-last2 = Wivel
    editor-first2 = Anders
    chapter = The Foreign Policy of Luxembourg
Entry #5: prefix: journal
    title = Luxembourg's Foreign Policy: Does Small Size Help or Hinder?
    last = Hey
    first = Jeanne A. K.
    date = 2002
    journal = Innovation: The European Journal of Social Science Research
    doi = 10.1080/1351161022000027621
    pmid = 
    issue = 3
    volume = 15
    pages = 211–225
Entry #6: prefix: book
    title = Small States in World Politics: Explaining Foreign Policy Behavior
    last = 
    first = 
    publisher = Lynne Rienner Publishers
    year = 2003
    isbn = 978-1-55587-920-4
    location = 
    pages = 
    editor-last = Hey
    editor-first = Jeanne A. K.
Entry #7: prefix: journal
    url = http://onsstad.vdl.lu/uploads/media/ons_stad_71-2002_2-4.pdf
    title = Les relations germano-luxembourgeoises durant les années 30
    last = Hoffmann
    first = Serge
    date = 2002
    journal = Ons Stad
    access-date = 21 January 2016
    issue = 71
    language = French
    trans-title = Germano-Luxembourgish relations in the 1930s
    archive-url = https://web.archive.org/web/20160913104553/http://onsstad.vdl.lu/uploads/media/ons_stad_71-2002_2-4.pdf
    archive-date = 13 September 2016
    url-status = dead
Entry #8: prefix: journal
    url = http://www.forum.lu/wp-content/uploads/2015/11/7631_329_Huberty.pdf
    title = Ein kleiner Fisch im großen Teich Luxemburger Verhandlungsstragien im Kontext der EU-Institutionen
    last = Huberty
    first = Martine
    date = May 2013
    journal = Forum
    doi = 
    pmid = 
    access-date = 
    issue = 329
    language = German
    pages = 21–23
    trans-title = A small fish in a big pond: Luxembourgish negotiation strategies in the context of EU institutions
Entry #9: prefix: journal
    url = http://www.forum.lu/wp-content/uploads/2015/11/5497_257_Kayser.pdf
    title = La neutralité du Luxembourg de 1918 à 1945
    last = Kayser
    first = Steve
    date = June 2006
    journal = Forum
    doi = 
    pmid = 
    access-date = 21 January 2016
    issue = 257
    pages = 36–39
    language = French
    trans-title = Luxembourg's neutrality from 1918 to 1945
Entry #10: prefix: journal
    title = La politique étrangère du Luxembourg
    last = Wurth
    first = Hubert
    date = 2006
    journal = Revue Internationale et Stratégique
    volume = 61
    doi = 10.3917/ris.061.0217
    pmid = 
    issue = 61
    pages = 217–226
    series = 
    language = French
    trans-title = Luxembourg’s Foreign Policy

0 голосов
/ 24 апреля 2020

Вы можете использовать 2 группы захвата и получать непрерывные совпадения, используя якорь \G. Использование класса отрицательных символов [^}]+ предполагает, что в ключах или значениях не может быть символа }.

(?:\{\{[^|]*|\G(?!^))(?=[^}]*}})\|([^=]+)=([^|}]+)

In Java

String regex = "(?:\\{\\{[^|]*|\\G(?!^))(?=[^\\}]*\\}})\\|([^=]+)=([^|\\}]+)";

Частями

  • (?: Группа без захвата
    • {{[^|]* Совпадение {{ с последующим сопоставлением с любым символом, кроме |
    • | Или
    • \G(?!^) Утверждение позиции в предыдущем матче, а не в начале
  • ) Закрытие группы
  • (?=[^}]*}}) Утверждение }} в правильные
  • \|\h* Совпадение | и 0+ горизонтальных пробелов
  • ([^=]+) Захват группа 1 , сопоставление с любым символом, кроме =
  • = Соответствует =
  • ([^|}]+) Захват группа 2

Regex demo | Java демо

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...