Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.util.regex.*;
- import java.io.*;
- public class LabelRef {
- public static void main(String[] args) throws IOException {
- Scanner sc = new Scanner(new File("input.txt"));
- Map<String, Integer> refLine = new HashMap<String, Integer>();
- Pattern labelPattern = Pattern.compile("\\\\label\\{([^}]*)\\}");
- int currentLine = 0;
- while (sc.hasNextLine()) {
- ++currentLine;
- String line = sc.nextLine();
- Matcher matcher = labelPattern.matcher(line);
- while (matcher.find()) {
- refLine.put(matcher.group(1), currentLine);
- }
- }
- Pattern refPattern = Pattern.compile("\\\\ref\\{([^}]*)\\}");
- sc.close();
- sc = new Scanner(new File("input.txt"));
- PrintWriter out = new PrintWriter("output.txt");
- while (sc.hasNextLine()) {
- String line = sc.nextLine();
- line = labelPattern.matcher(line).replaceAll("");
- while (true) {
- Matcher matcher = refPattern.matcher(line);
- if (!matcher.find()) {
- break;
- }
- int lineNumber = refLine.get(matcher.group(1));
- line = matcher.replaceFirst("" + lineNumber);
- }
- out.println(line);
- }
- out.close();
- sc.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment