ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm working on a school project written in Objective-C, but hopefully this is a simple thing...
I have an XML document. I need to go into that document, find instances of timecode (like 00:01:22:12) and replace them with a string, like "foobar". The timecode will always be different numbers, but always in the same format.
My problem is I've never done regEx in C before... anyone know how this is done?
Don't bother with regex. Read the XML file line-by-line and use sscanf() to replace the timestamp with the desired alternative. If the name of the XML element preceding the timestamp is constant and unique, you could even use strstr() to get where the stamp starts and ends and modify it from there.
I started writing an example use of <regex.h> and then I realized it's too much work for something that can be easily avoided.
Are you sure pattern matching is robust enough? Since you're using some XML data, shouldn't you be parsing the XML and locating the relevant data that way?
One of the reasons to use XML in the first place is to provide structure that allows you to find your data. Having said that, perhaps using R-E's is part of your assignment. In that case, you would use the functions declared in regex.h. For the full explanation of regex usage in C,
Code:
man 3 regex
A previous thread on the subject may be illustrative.
The basic premise is that your code contains a literal (or other, I guess) string containing a regular expression notation. This string is then 'compiled' and the resulting handle is used to reference the compiled regex in one or more searches of a specified buffer.
Now, there is another alternative which I think is loads more fun and instructive, and that is to write a simple lexical analyzer using lex. This generates C code, which you compile, link, & run against your XML file(s).
--- rod.
Don't bother with regex. Read the XML file line-by-line and use sscanf() to replace the timestamp with the desired alternative. If the name of the XML element preceding the timestamp is constant and unique, you could even use strstr() to get where the stamp starts and ends and modify it from there.
A sample xml file would indeed help.
Sounds great to me, if I can avoid regEx I will gladly do so. This is a group project, and I'm not the one generating the XML file, and I don't have the XML file in front of me right now, but here is the kind of data we would we doing:
and so on. it would all be very standardized; the value for "media start" would ALWAYS be "in ##:##:##:##" for instance. So what you're saying sounds like it's the way to go. I will look into sscanf() but if you want to post an example or suggestion, you're welcome to do so!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.