0

I'm a total begginner when it comes to Regex , if any one has a suggestion for a good place to learn more about re Module, I would realy appreciate it .

As for my question , Basically i'm playing around with it to find something like this :

$letters(content)
  • no \s or \t between $ and letters

  • letters are either UPPER or LOWER or _ or Numbers (But not at start)

  • Possible \s or \t between letters and (

  • Must Ends with )

As for content :

  • can Have : UPPERCASE , LOWERCAE , DIGIT , $letter2(..), 'word', "word" , , , +-*/%! , .

You can Imagine it as a Function Calling in this case i guess .

so in Brief , How can i achieve finding such Prototypes :

$Operation(10, $l_shift_byte($random(20)), "addition")

or

$MessageBox(0, "Hello There Bug Boi !\nYour Lucky Number is : "+ $toString((10 - $random(20))), MB_OK)

---------- [ EDIT / UPDATE ] ----------

what i tried (From what Stefan Said ):

^(\s+\$|\$)[A-Za-z_]+(.*)

which works fine but as long as there are no \n inside the ( .. ) example of a not working case where the match stops at \n :

$operation( 10, $l_shift_byte($random(20)), "\naddition")

Also Kept Messing with it and got to this :

^(\s+\$[A-Za-z_]+|\$[A-Za-z_]+)[0-9A-Za-z_]+(\s+\(|\()[\w\W]+(\)|\)\s)$

which Also not working as expected , for example if i try to match this :

$ThisIsATest() $operatio8n_s ( 10, $l_shift_byte($random(20)), " \naddition")$quit()

it Returns everything , since it starts with $acceptable_ch4ars ( ... and ends with ) , wherase it should returns :

[
    '$ThisIsATest()',
    '$operatio8n_s   ( 10, $l_shift_byte($random(20)), " \naddition")',
    '$quit()'
]

Thank you in Advance!

Saad Amrani
  • 95
  • 2
  • 11

1 Answers1

1

A good place to start is always the documentation of the module re.

Coming to your case, a possible regex is:

^\$[A-Za-z]+\(.*\)

which finds both of your example strings.

  • ^: matches the start of the string (you might want to remove this)
  • \$: matches the $, note that it is escaped using \
  • [A-Za-z]+: matches 1 or more lower or upper case letters
  • \( and \): match the enclosing brackets
  • .*: Matches everything except newline 0 or more times

When it comes to regex, online regex tester might become handy.

Stefan
  • 1,697
  • 15
  • 31
  • Pretty usefull Link , And very Well explained ! Thank you very much . From what you explained I managed to optimize it a little and This is what i ended up with **^.\s\$|\$+[A-Za-z]+\(.*\)** Works pretty fine , Thank you ! Waiting in case there are some more detailed Answers . – Saad Amrani Aug 22 '20 at 06:08
  • Both regex are working when I try them (with python3). After importing `re` I use `re.match(regex,test_string).group(0)` to show the entire match. – Stefan Aug 22 '20 at 06:35
  • Can you check this : [PY3_CODE](https://onlinegdb.com/Bycd1HCzD) , it works fine but once i add \n or some spaces it does not , How ever it works fine in that TestingRegex Website . – Saad Amrani Aug 22 '20 at 06:48
  • I would split the string by newline, loop over the individual lines and check if those match your pattern. – Stefan Aug 22 '20 at 07:27
  • 1
    You can also remove the `^` which actually matches the start of the string, since the string from your link starts with a whitespace and not with a `$` there is no match. I updated my answer to make it more clear. – Stefan Aug 22 '20 at 07:30
  • I know but the main problem is on things such as : `$operation( 10, $l_shift_byte($random(20)), "\naddition")` notice the \n in that string ? well that a problem Because the match stops right there Unless i replace it first with some special char then try to match the regex and put \n back (which actually works ).... But thats a bit much thing to do in my case . – Saad Amrani Aug 22 '20 at 07:40
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/220253/discussion-between-saad-amrani-and-stefan). – Saad Amrani Aug 22 '20 at 07:45
  • Remove the `^` in the regex I gave and I think it does what you want... – Stefan Aug 22 '20 at 07:56
  • Not working buddy , ^ is not a problem , i managed to work it out so it matches either it with $ or \s+\$ , please read well what i said in my last comment , Untill we found a solution i'm accepting further answers . – Saad Amrani Aug 22 '20 at 08:08
  • 1
    Ok, then please specify better in your question what you actually want to have instead of coming up with new stuff every time you comment. The answer I provided matches all the strings you gave in your question... – Stefan Aug 22 '20 at 08:18
  • Stefan , My bad I forgot about that , I updated it , please give some time to it and check it out . Thanks. – Saad Amrani Aug 22 '20 at 08:41