Solved

Newbie Regex Question

Posted on 2008-10-02
11
243 Views
Last Modified: 2012-05-05
I have the following strings possible:
<division /> some text string
some text string <division /> some text string
some text string <division />.

I need to replace the string <division /> with <global>division</global>
BTW, <division /> is just one example...others could be <companyname />, <attention /> etc.
so I would like to replace the < and the /> with <global></global>
just not having much success...
0
Comment
Question by:cfmdev
  • 6
  • 3
  • 2
11 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 22624991
.. replace the < and the /> with <global></global>

Instead of trying to use REGEX, why not just use string replacement?  Easier, I would guess! ~Ray

0
 

Author Comment

by:cfmdev
ID: 22625206
Well...I could do that, but the string could also contain this:
<BULLETLIST><BULLET>some text value <division /></BULLETLIST>
I should have made that clear...so I guess what I need is a way to tell if < & /> are in the same string? sorry to be so dense...
0
 

Author Comment

by:cfmdev
ID: 22625907
One other thing...the string <division /> could also be more than one word...like <company name />...that throws a wrench into also...
0
 

Author Comment

by:cfmdev
ID: 22626265
Ok...here is my solution but I don't think it is that elegant...hoping someone else has a way to simplify it:

<cfset str = "My company: TEST <company name />">

<cfset str = rereplace(str,'(<.[^(><.)]+>)','<global>\1</global>',"ALL")>

<cfset str = rereplace(str,'><','>',"ALL")>
 
<cfset str = rereplace(str,' />','<',"ALL")>
 
does the trick but can it be improved?
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 22626890
Unless you are doing thousands of these per minute, I would not worry much about improving the process.  It may not look "elegant" on paper, but it's effective and easy to understand! ~Ray
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:cfmdev
ID: 22627338
Ok Ray...thanks for your input...
0
 
LVL 5

Expert Comment

by:zmo
ID: 22693981
I don't know any coldfusion, but in pure regexps what you want would be :

'' s,<division />,<global>division</global>,g ''

(try it with :
 echo "some text string <division /> some text string" | sed "s,<division />,<global>division</global>,g"
on a unix command line)

usually most languages give a regexp module that can process regexps like that...

the syntax of this is :

s/<string to be replaced>/<replacement string>/

the '/' character has to be there 3 times but can be replaced by any character (that's the position and the repetition of that character that is important) and you can append 'g' at the end, so every occurence of the string on a line is replaced.

Hope it'll be useful for you and given what you want ;)
0
 

Author Comment

by:cfmdev
ID: 22703205
zmo...doesn't seem to work correctly in the coldfusion rereplace function that I need to use...but I have a solution that I came up with that seems to have done the trick:
<cfset str = "My division name is: <division />">
<cfset str = rereplace(str, '(<[A-Za-z0-9]+ />)','<global>\1</global>','ALL')>
<cfoutput>#str#</cfoutput>
0
 
LVL 5

Expert Comment

by:zmo
ID: 22704178
well, that was obvious it wouldn't work as is... what I told you is the "general" (ie POSIX) regexp syntax.
Glad to see you found your way in their implementation...

And as far as I can see, they've done a pretty neat implementation of standard regexps :)
0
 
LVL 5

Accepted Solution

by:
zmo earned 500 total points
ID: 22704205
btw, you could use some kind of '(division|companyname|attention|whatever)' regexp to define exactly what you want to match, if you don't need to match all tags...
0
 

Author Closing Comment

by:cfmdev
ID: 31502332
Yea...that's a good suggestion...I've been spending a lot of time lately getting up to speed with regex's and I'm seeing that there are often multiple solutions to a problem...a great resource I'm using is the o'reilly book "Mastering Regular Expressions". Thanks zmo for your input...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to gather data from news site 6 32
Video/Image Streaming/Desktop Sharing 6 75
How to save ebook from https://books.google.com/ 4 104
WCAG audit tools 1 72
One of Google's most recent algorithm changes affecting local searches is entitled "The Pigeon Update." This update has dramatically enhanced search inquires for the keyword "Yelp." Google searches with the word "Yelp" included will now yield Yelp a…
Article by: Nadia
Suppose you use Uber application as a rider and you request a ride to go from one place to another. Your driver just arrived at the parking lot of your place. The only thing you know about the ride is the license plate number. How do you find your U…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now