![]() |
any regex pros here? - Printable Version +- MacResource (https://forums.macresource.com) +-- Forum: My Category (https://forums.macresource.com/forumdisplay.php?fid=1) +--- Forum: Tips and Deals (https://forums.macresource.com/forumdisplay.php?fid=3) +--- Thread: any regex pros here? (/showthread.php?tid=64319) |
any regex pros here? - wolfcry911 - 10-17-2008 I've used regex very little and I'm having trouble figuring this one out. With the following two, I'd like either the full string, or the part following the '::' if it exists "Location" "Inspections_Selected::Inspector" would return "Location" "Inspector" TIA Re: any regex pros here? - TheCaber - 10-17-2008 In other words, you want to strip out everything from the beginning of the line through the '::' (if it exists) and output the remainder of the line. sed -E 's/^.*:://' will do this. In the Terminal, try: $ (echo Location ; echo Inspections_Selected::Inspector) | sed -E 's/^.*:://' Location Inspector $ Re: any regex pros here? - wolfcry911 - 10-17-2008 yes Re: any regex pros here? - Seacrest - 10-17-2008 I struggle with all but the simplest regexes myself, so I usually find pre-configured ones online. This is a good place to experiment, though: http://www.fileformat.info/tool/regex.htm. Re: any regex pros here? - wolfcry911 - 10-17-2008 wow, thanks guys. TheCaber, perfect! thanks a bunch. Seacrest, that site will come in handy - ty Re: any regex pros here? - wolfcry911 - 10-18-2008 TheCaber, would this do the same thing? \b\w+(?! ![]() Re: any regex pros here? - TheCaber - 10-18-2008 Um, you never specified which language you were using to code the regular expressions. I gave the 'old school' UNIX/POSIX sed (stream editor) BRE (basic regular expressions) form. Your latest post is clearly 'new school'. According to http://www.greenend.org.uk/rjk/2002/06/regexp.html , it is one of Perl, Python or Tcl, since it involves negated 'lookahead'. I'll guess it is Perl. \b (match boundary at beginning of word) \w+ (match one or more word-class characters (alphanumeric or underscore)) ( ? ! : ) (next character is not a colon) (I embedded spaces to hold off the smilies) \b (match boundary at end of word) I think this will not eat the second colon, nor will it deal with embedded spaces or other non-word-class characters. FWIW, Re: any regex pros here? - TheCaber - 10-18-2008 Here's one way to do it in Perl (there are many ways, most superior to this). In the Terminal: (First be sure we're in the Bourne shell, not csh or bash) % sh sh-3.2$ sh-3.2$ ( echo Location ; echo Inspections_Selected::Inspector ) | perl -e 'while ( <> ) {; print if s/(?:\b\w+::\b)?(.*)$/$1/; };' Location Inspector sh-3.2$ Ugly. Sorry. Re: any regex pros here? - wolfcry911 - 10-18-2008 Sorry I should have been more specific. Yes, I failed to mention what language this was in. Truth be told, I'm in over my head anyway ![]() As it turns out, I may not be able to do this anyway. I was trying to use regex inside an xslt file. I'm trying to transform an xml file into an fdf file for a pdf form. I want the fdf field name to take on the name attribute of the xml field element, but I don't want to include the table portion of said name. This is all new to me, but I feel I'm very close to a solution - in fact, I have it working if I hard code the field names, but I don't feel that's very wise (in case the xml is formed different than expected). |