-6

I have a block of text like:

# <init> (Lorg/cyanogenmod/audiofx/ActivityMusic;)V
         , 0 iput-object v1, v0, Lorg/cyanogenmod/audiofx/ActivityMusic$11;->this$0 Lorg/cyanogenmod/audiofx/ActivityMusic;
         , 4 invoke-direct v0, Ljava/lang/Object;-><init>()V
         , a return-void 

I would like a reg-exp in Python that will select only the first line

# <init> (Lorg/cyanogenmod/audiofx/ActivityMusic;)V

Is this possible? Thanks!

4oxer
  • 225
  • 1
  • 4
  • 6

1 Answers1

1

Just split by a newline and get the first element:

test_str = "# <init> (Lorg/cyanogenmod/audiofx/ActivityMusic;)V\n         , 0 iput-object v1, v0, Lorg/cyanogenmod/audiofx/ActivityMusic$11;->this$0 Lorg/cyanogenmod/audiofx/ActivityMusic;\n         , 4 invoke-direct v0, Ljava/lang/Object;-><init>()V\n         , a return-void "
print(test_str.split('\n')[0])

See demo

Output: # <init> (Lorg/cyanogenmod/audiofx/ActivityMusic;)V.

If it is not the first line:

A non-regex way:

test_str = "some string\n# <init> (Lorg/cyanogenmod/audiofx/ActivityMusic;)V\n         , 0 iput-object v1, v0, Lorg/cyanogenmod/audiofx/ActivityMusic$11;->this$0 Lorg/cyanogenmod/audiofx/ActivityMusic;\n         , 4 invoke-direct v0, Ljava/lang/Object;-><init>()V\n         , a return-void\n# <init> (Lorg/cyanogenmod/audiofx/ActivityMusic;)V "
ss = test_str.split('\n')
for s in ss:
    if s.strip()[0] == "#":
        print s
        break

For a regex way, see this:

p = re.compile(r'^#.*', re.M)
print p.search(test_str).group()

The main point in the regex approach is

  • Use re.M multiline flag
  • Use re.search that will return just 1 match object
  • The # must be the first character (or add \s* - optional whitespace) so that the line starting with it could be matched.
Community
  • 1
  • 1
Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
  • 1
    what if it not the first line? – Padraic Cunningham Aug 06 '15 at 21:24
  • I am a kind person willing to help people, but downvoting in a couple of seconds after posting an answer that actually works ... I start losing faith. – Wiktor Stribiżew Aug 06 '15 at 21:33
  • I think the dv was most likely because the person did not like the question more than they disliked your answer. – Padraic Cunningham Aug 06 '15 at 21:38
  • And this is the common bad practice: I do not like the question, I will hurt those who help. – Wiktor Stribiżew Aug 06 '15 at 21:40
  • I offset the dv, personally i would not worry too much about it. – Padraic Cunningham Aug 06 '15 at 21:51
  • @stribizhev I think you're taking this too personally. I often downvote answers in the way yours was (though I didn't downvote yours) because I think answering bad questions encourages them and makes it harder for the system to clean them up. Also, one downvote when you have 32,000 rep is hardly "hurting" you, is it? – Two-Bit Alchemist Aug 06 '15 at 21:52
  • @Two-BitAlchemist: Right, I am taking that personally. I am not against deleting it, just want to know if OP is willing to delete the question. Just people have different styles of answering, it is not fair to assess the answer during the first 10 seconds, we are humans and typing (and sometimes, realizing) takes time. – Wiktor Stribiżew Aug 06 '15 at 21:59
  • @4oxer: Glad it worked or you. Please also consider upvoting the answer if it proved helpful to you. – Wiktor Stribiżew Aug 06 '15 at 22:15