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:
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.
8
solved Python Regex starting with hashtag