LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-09-2008, 10:33 AM   #1
harkonen
Member
 
Registered: May 2007
Distribution: slackware
Posts: 46

Rep: Reputation: 16
Regular Expression


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?
 
Old 07-09-2008, 02:31 PM   #2
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Show examples of your data. If the data cannot be unambiguously determined, a regex will be difficult if not possible.

Last edited by Mr. C.; 07-10-2008 at 03:10 PM.
 
Old 07-10-2008, 12:10 PM   #3
yousifnet
LQ Newbie
 
Registered: May 2008
Posts: 3

Rep: Reputation: 0
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.

A sample xml file would indeed help.
 
Old 07-10-2008, 01:21 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
And if those times appear in different tags?
 
Old 07-10-2008, 02:13 PM   #5
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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?
 
Old 07-10-2008, 03:53 PM   #6
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.
 
Old 07-12-2008, 12:06 PM   #7
harkonen
Member
 
Registered: May 2007
Distribution: slackware
Posts: 46

Original Poster
Rep: Reputation: 16
sample XML data

Quote:
Originally Posted by yousifnet View Post
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:

<KEY>media start</KEY>
<STRING>in 00:01:23:22</STRING>
<KEY>media end</KEY>
<STRING>out 00:01:45:15</STRING>

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!

Thanks for all the help guys, I appreciate it.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
regular expression (.*?) uttam_h Programming 6 05-30-2008 05:45 PM
Do we have regular expression in C++ ? indian Programming 4 03-06-2006 09:54 AM
Regular expression datbenik Programming 1 01-05-2006 01:58 PM
Anyone know regular expression? ahhua Linux - Software 1 12-04-2003 08:13 AM
regular expression gumby Programming 3 07-15-2003 12:13 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration