|
Mailing Lists
|
Home /
Groups /
ColdFusion Talk (CF-Talk)
Regex Q
Author: Barney Boisvert
Short Link: http://www.houseoffusion.com/groups/cf-talk/thread.cfm/threadid:32463#162988
Responses inline:
> ok, lemme try and break this down:
>
> ^[^<] anything at the beginning of the string that is NOT an opening
> bracket (which would take care of the FirstName LastName and
> any spaces)
>
> +< an opening bracket
Not quite right; should be like this:
^[^<]+ one or more characters at the beginnging that ar enot openeing
brackets. That should be a * not a + as well, to handle the case that the
first char is the "<".
< an opening bracket
> ([^>]+) this is where I start to get lost. I know the [^>]
> is anything
> that is not a closing bracket...but not sure where the +
> comes in inside of
> the parentheses.
Working from inside out, [^>] means anything except a closing bracket, the +
means one or more such characters, and the parentheses mean "store the
enclosed value for backreferencing".
> >.*$ a closing bracket, and anything that might follow it
> (such as trailing
> spaces).
Bingo!
> Also, I know the \1 is a 'backreference'...just not exactly
> sure what that
> means. If you could clarify the ([^>]+) and the \1 I'd be eternally
> grateful :)
When we stored the value using parantheses it gets stored with a number
reference. Since it's the first such value (you can have multiple), it gets
to be "1". We can reference that value anywhere later in the regular
expression, or in the replace string by prepending it with a backslash.
Here's some more examples:
Rereplace("catdog", "cat(.*)", "\1\1") => dogdog
Rereplace("'dog'", "(['""])(.*)\1", "\1\2\2\1") => 'dogdog'
Rereplace("""dog""", "(['""])(.*)\1", "\1\2\2\1") => "dogdog"
Rereplace("'dog""", "(['""])(.*)\1", "\1\2\2\1") => 'dog"
In the first case, we double whatever is after 'cat'. In numbers 2-4 we use
the same RE and search stirng for all three. It searches for a single or
double quote and stores it, then 'anything' and stores it, then a MATCHING
quote (using the backreference). We replace it with the same quote, and the
'anything' twice, and then the quote again. #2 uses single quotes, #3 uses
double quotes, and #4 uses mixed quotes, which DOESN'T work, so the replace
doesn't happen.
Cheers,
barneyb
Author: Dick Applebaum
Short Link: http://www.houseoffusion.com/groups/cf-talk/thread.cfm/threadid:32463#162983
Oops---
<cfset email = ReReplaceNocase(email, ".*<([^>]*)>.*", "\1")/>
is better
On May 12, 2004, at 10:10 AM, Dick Applebaum wrote:
----- Excess quoted text cut - see Original Post for more -----
Author: Charlie Griefer
Short Link: http://www.houseoffusion.com/groups/cf-talk/thread.cfm/threadid:32463#162982
ok, lemme try and break this down:
^[^<] anything at the beginning of the string that is NOT an opening
bracket (which would take care of the FirstName LastName and any spaces)
+< an opening bracket
([^>]+) this is where I start to get lost. I know the [^>] is anything
that is not a closing bracket...but not sure where the + comes in inside of
the parentheses.
>.*$ a closing bracket, and anything that might follow it (such as trailing
spaces).
Also, I know the \1 is a 'backreference'...just not exactly sure what that
means. If you could clarify the ([^>]+) and the \1 I'd be eternally
grateful :)
Charlie
> This'll do it:
>
> Rereplace(email, "^[^<]+<([^>]+)>.*$", "\1", "one")
>
> I threw the extra ".*" in there at the end because the possibility of
having
> trailing spaces is always there, and we can clean it up at the same time,
so
----- Excess quoted text cut - see Original Post for more -----
Author: Dick Applebaum
Short Link: http://www.houseoffusion.com/groups/cf-talk/thread.cfm/threadid:32463#162981
<cfset email = ReReplaceNocase(email, ".*<([^>]*)>.?", "\1")/>
HTH
Dick
On May 12, 2004, at 9:58 AM, Charlie Griefer wrote:
----- Excess quoted text cut - see Original Post for more -----
Author: Barney Boisvert
Short Link: http://www.houseoffusion.com/groups/cf-talk/thread.cfm/threadid:32463#162980
This'll do it:
Rereplace(email, "^[^<]+<([^>]+)>.*$", "\1", "one")
I threw the extra ".*" in there at the end because the possibility of having
trailing spaces is always there, and we can clean it up at the same time, so
why not?
Cheers,
barneyb
----- Excess quoted text cut - see Original Post for more -----
Author: Charlie Griefer
Short Link: http://www.houseoffusion.com/groups/cf-talk/thread.cfm/threadid:32463#162978
On another forum, a user asked how to turn this string:
FirstName LastName <somebody@somewhere.com>
into this string:
somebody@somewhere.com
I was able to come up with:
<cfset email = reReplace(left(email, len(email)-1), '(^.*<)', '',
'all')>
I feel a little cheesy using the left() function to remove that last
bracket, as I'm sure there must have been a way within the same regex.
I tried a variety of things, such as adding .$ and even the literal > to the
expression above, but I assume that means it was looking for that expression
as an expression itself. But there has to be a way to say, replace (this
exp) AND (that exp)? or would it just be a case of nesting reReplace()
functions?
Charlie
-still trying to get a handle on regex
|
May 24, 2012
|
Latest Fusion Authority Articles
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||