Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
any regex pros here?
#1
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
Reply
#2
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
$
Reply
#3
yes
Reply
#4
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.
Reply
#5
wow, thanks guys.
TheCaber, perfect! thanks a bunch.
Seacrest, that site will come in handy - ty
Reply
#6
TheCaber, would this do the same thing?

\b\w+(?!Smile\b
Reply
#7
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,
Reply
#8
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.
Reply
#9
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 Wink . I did know what sed was though (I have a buddy who's a Unix guru).

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).
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)