简体   繁体   中英

C equivalent to java.util.regex

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 ?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM