This is for an assignment. If anybody else who is doing this assignment finds this code then please don’t copy it.
EDIT: Apologies, as this work is now copyable, please credit me and I will ask my professor more on what he thinks of the matter.
So I have some file which contains something like this
public Test();
Code:
Stack=1, Locals=1, Args_size=1
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: return
LineNumberTable:
line 3: 0
public static void main(java.lang.String[]);
Code:
Stack=3, Locals=3, Args_size=1
0: new #2; //class java/util/Scanner
3: dup
4: getstatic #3; //Field java/lang/System.in:Ljava/io/InputStream;
7: invokespecial #4; //Method java/util/Scanner."<init>":(Ljava/io/InputStream;)V
10: astore_1
11: aload_1
12: invokevirtual #5; //Method java/util/Scanner.nextLine:()Ljava/lang/String;
15: astore_2
16: getstatic #6; //Field java/lang/System.out:Ljava/io/PrintStream;
19: aload_2
20: invokestatic #7; //Method add_periods:(Ljava/lang/String;)Ljava/lang/String;
23: invokevirtual #8; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
26: return
LineNumberTable:
line 6: 0
line 8: 11
line 9: 16
line 10: 26
and I am trying to separate the output so that the first output will be the following.
public Test();
Code:
Stack=1, Locals=1, Args_size=1
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: return
LineNumberTable:
line 3: 0
and the second output would be the second method. Assume that I can’t use split using double new line characters as delimiters due to the fact that some of the output not shown here will be grabbed that I would rather not grab.
I have a regular expression that looks like the following.
files.scan(/.*\)\;\n(.+\n)*/)
What the regular expression is trying to do is the following:
The first part of the regular expression .*\)\; is trying to match the method name and it works fine.
The second part is supposed to match every single line after it and stop until it sees a double new line character at which stage it fails due to there being a double new line character.
What it returns instead is the last line of every single method and I don’t know why.
The same regex in python as shown below gets the whole piece of code but this doesn’t
ANSWER=re.search(r'.*\);\n(.+\n)*', STRING)
Can anyone explain why it isn’t working?
As this is an assignment after all please don’t give code to solve the problem that I am trying to do. I appreciate that, thanks.
The problem is that
scanreturns an array of only the group matches (the parenthesized parts of the regexp) if there are any groups. To avoid the parenthesized parts becoming a group you can modify your regexp to use(?:...)instead of(...).scanwill return an array of the whole matches.Or, you can hand over a block to
scanwhich has access to the groups as well as to the whole match through the variables$1,$2, … and$&.See Ruby’s doc for scan.