niyaznigmatullin

Untitled

Dec 19th, 2014
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.15 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.regex.*;
  3. import java.io.*;
  4.  
  5. public class LabelRef {
  6.     public static void main(String[] args) throws IOException {
  7.         Scanner sc = new Scanner(new File("input.txt"));
  8.         Map<String, Integer> refLine = new HashMap<String, Integer>();
  9.         Pattern labelPattern = Pattern.compile("\\\\label\\{([^}]*)\\}");
  10.         int currentLine = 0;
  11.         while (sc.hasNextLine()) {
  12.             ++currentLine;
  13.             String line = sc.nextLine();
  14.             Matcher matcher = labelPattern.matcher(line);
  15.             while (matcher.find()) {
  16.                 refLine.put(matcher.group(1), currentLine);
  17.             }
  18.         }
  19.         Pattern refPattern = Pattern.compile("\\\\ref\\{([^}]*)\\}");
  20.         sc.close();
  21.         sc = new Scanner(new File("input.txt"));
  22.         PrintWriter out = new PrintWriter("output.txt");
  23.         while (sc.hasNextLine()) {
  24.             String line = sc.nextLine();
  25.             line = labelPattern.matcher(line).replaceAll("");
  26.             while (true) {
  27.                 Matcher matcher = refPattern.matcher(line);
  28.                 if (!matcher.find()) {
  29.                     break;
  30.                 }
  31.                 int lineNumber = refLine.get(matcher.group(1));
  32.                 line = matcher.replaceFirst("" + lineNumber);
  33.             }
  34.             out.println(line);
  35.         }
  36.         out.close();
  37.         sc.close();
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment