0

I've built a program in Java to translate a assembly code to machine code, I'm using this regex to do it:

^((?<label>.*):)?\s*(?<instruction>\w+)(\s+(?<op1>\w+))?(\s+(?<op2>\w+))?(;\s+(?<comment>.*))?$

I've tried to use standard regex.h, if I take the group names out it compiles but does not work because the optional groups ()? are treated as select groups...

It's really hard to search in Google because everything is about C# and not C. I understand that Java, Python, JavaScript or even C# would make my life easier, but I need to do it in C. If I can't use regex, it will become a sscanf challenge.

Is there a way to convert this regex to C regex or there is a C equivalent for java.util.regex?

1
  • 2
    I don't believe C, as distinct from C++, has a standard regex library. So you need to tell us what library you're using. (For instance, the GNU one? If so, which interface?) Commented Oct 26, 2015 at 0:55

2 Answers 2

3

I've had some luck using PCRE for complicated regexes from C or C++. It's pretty widely used and compliant. It used to have some issues with unicode data, but it looks like some of those have been resolved now.

PCRE supports named captures as used in your example using the pcre_copy_named_substring function.

Sign up to request clarification or add additional context in comments.

2 Comments

Anyway, you read the regex incorrectly. (?<label>.*) is a capturing group, named label, which captures text matched by .*. Same for (?<op1>\w+), which is a capturing group labelled op1, capturing text for \w+. Do note that the OP is writing the regex in Java. As far as I can see, the regex can be taken over to PCRE without change.
Ugh thanks @nhahtdh - too early in the morning for me ;) But this is good news since PCRE supports named capture groups too... So I'll update accordingly.
0

I am not sure how much this would help and how much work you would have to put into this. But why don't you take a look at the Java source code for java.util.regex.Pattern to see how this is build. If that's what you want, then you just have to translate into C.

Comments

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.