2

I want read out variable which is mentioned below in file.

#define xyz_u8 abc_0x0_u8 = 0x0 (for hex)
#define xyz_f16 abc_MOD1_f32 = -0.1f (for int and float)
#define xyz abc_YY = YY_ZZ (for others)

I am using

re.compile(r"^#define\s+(\w+)\s+(\w+)(0[xX][0-9a-fA-F]+)")

for reading hex, but i am not able to get value from '='. Can anyone correct the regular expression.

6
  • 3
    What about a mere .* or .+ at the end? Try (?i)^#define\s+(\w+)\s+(\w+) = (.+) Commented Jul 11, 2016 at 7:22
  • Thanks wiktor .it worked. Commented Jul 11, 2016 at 7:28
  • 1
    Great, I added an answer with explanation. Commented Jul 11, 2016 at 7:31
  • Wiktor, How can i find regex for 'SET_abc_1(GET_xyz_2())' i am trying as re.compile(r"^(SET\w+)(GET\w+)") but it is not searching. Also i want to find variable like this "SET_abc(GET_xyz()/2 + GEt_qwe_3()/2)" Commented Jul 13, 2016 at 11:03
  • Isn't it a new question already? No idea what you need, really. Try (SET\w+)\((GET\w+)(?:(?:\([^()]*\)/\d+)?\W+(GET\w+))?. Commented Jul 13, 2016 at 11:19

1 Answer 1

1

I suggest

^#define\s+(\w+)\s+(\w+) = (.+)

Use it with the re.I and re.M (if you have multiline input).

See the regex demo

Pattern explanation:

  • ^ - start of string (or line if re.M is used)
  • #define - a sequence of literal characters
  • \s+ - 1 or more whitespaces
  • (\w+) - Group 1 capturing one or more word ([a-zA-Z0-9_]) chars
  • \s+ - 1 or more whitespaces
  • (\w+) - Group 2 capturing one or more word ([a-zA-Z0-9_]) chars
  • = - space, =, space (you may add + after each space to allow 1 or more spaces, or * to allow 0 or more spaces)
  • (.+) - Group 3 that will capture the rest of the line.
Sign up to request clarification or add additional context in comments.

1 Comment

If the answer worked for you please consider accepting it. If you have a new question, please ask.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.