I'm trying to pull a youtube ID from a link like this;
<img src="http://img.youtube.com/vi/OZ3jyvM0jZc/2.jpg" alt="" />
I've only been successful in taking out the ID, but not actually getting it!
<cfset ytID = '<img src="http://img.youtube.com/vi/0Z3jyvM0jZc/2.jpg" alt="" />' />
#reReplace(referer,"(vi=?(\=|\/))([-a-zA-Z0-9_]+)|(vi=\/)([-a-zA-Z0-9_]+)", "\1", "one")#
Output: <img src="http://img.youtube.com/vi//2.jpg" alt="" />
RegEx is not my friend today. What am I missing?
Thanks!
Try with regex:
vi\/([^\/]+) // 0Z3jyvM0jZc
You don't need to escape the forward slashes in CFML regex patterns. So take what The Mask has and use whichever method you prefer (both of these only work if the string is indeed a match):
<cfset ytID = '<img src="http://img.youtube.com/vi/0Z3jyvM0jZc/2.jpg" alt="" />'>
<cfoutput>
<pre>
<cfset sLenPos=REFind("/vi/([^/]+)", ytID, 1, "True")>
#mid(ytID, sLenPos.pos[2], sLenPos.len[2])# == OZ3jyvM0jZc
#reReplace(ytID,".*/vi/([^/]+)/.*", "\1")# == OZ3jyvM0jZc
</pre>
</cfoutput>
The key to keeping this simple is using the [^/]+ to match one or more characters that aren't /
I think regex might be the wrong tool for this job. How about using lists?
<cfset ytStr = '<img src="http://img.youtube.com/vi/0Z3jyvM0jZc/2.jpg" alt="" />'>
<cfset ytID = ListGetAt(ytStr, 4, '/')>
<cfset ytID = '<img src="http://img.youtube.com/vi/0Z3jyvM0jZc/2.jpg" alt="" />'>
<cfset sLenPos=REFind("(vi=?(\=|\/))([-a-zA-Z0-9_]+)|(vi=\/)([-a-zA-Z0-9_]+)", ytID, 1, "True")>
<cfoutput>
#mid(ytID, sLenPos.pos[1], sLenPos.len[1])#
</cfoutput>
Related
Here my scenario I want to get the cid value without cid: from the img src in mail content. I've the inline image code like <img src="cid:ii_k4ib6vux0" alt="image.png" width="195" height="162">.
Now I want to get the cid value ii_k4ib6vux0. When I try to use the regex cid([^""']+) I've got the value with cid like cid:ii_k4ib6vux0. But I want to get the value only. Please guide me to get the exact values.
Thanks in advance!
You could try this:
<cfset aString = '<img src="cid:ii_k4ib6vux0" alt="image.png" width="195" height="162">' />
<cfset aMatch = REMatch('"cid:([^"]*)"', aString) />
<cfdump var="#replace(aMatch[1], "cid:", "")#" />
Example:
https://trycf.com/gist/9202b3dd2cca2cf0341a594dc007644f/acf2016?theme=monokai
Update:
Using REFind
<cfset aString = '<img src="cid:ii_k4ib6vux0" alt="image.png" width="195" height="162">' />
<cfset aMatch = REFind('"cid:([^"]*)"',aString,1,true,"ALL") />
<cfdump var="#aMatch[1].match[2]#" />
https://trycf.com/gist/809a30fdc16cc6deac6d1034dfb8adc2/acf2016?theme=monokai
In order to capture only that code and not having to check the groups (parts between parenthesis), you can use a positive lookbehind:
(?<=cid:)[^"']+
This will match any character besides ' and " which come after cid:.
Otherwise, using a regex similar to yours (note the : and the removed "):
cid:([^"']+)
You need to check the first group. Depending on what language you are using, the first group may be the whole captured string and you may need to check the second group.
I have attribute like this.
Example:
<div class="issue-document" id="assr_0335-5985_1991_num_76_1_1619" itemprop="hasPart" itemscope="" itemtype="https://facebook.com>
<cfset mystring = 'This is some text. It is true that <div class="issue-document" id="assr_0335-5985_1991_num_76_1_1619" itemprop="hasPart" itemscope="" itemtype="https://facebook.com">Harry Potter</div> is a good, but is better'>
<cfset MyReplace = ReReplaceNoCase(mystring,"<div [^>]*>","","ALL")>
<cfoutput><pre>Original string: #mystring#
Without link: #myreplace#</pre></cfoutput>
I need to remove only itemscope and itemprop like this rest of the attribute like id,class, style i don't want to remove using coldfusion in regular expression. Can any one please help to find the solution.
<cfset myReplace = reReplaceNoCase(mystring, '\b(itemscope|itemprop)="[^"]*"', "", "all")) />
https://trycf.com/gist/8d84d7d355f7c54e5533eeed22d097ba/acf2016?theme=monokai
i need some help here with regards to replace or maybe rereplace
i am trying to replace the font-family:anything to font-family:swiss7
but if there is a value font-family: BebasNeue; i want that font untouched and do not add font-size, but add the font-size to the other fonts
I am following this tutorial but somehow its not matching as to what I need to achieve
https://www.sitekickr.com/snippets/coldfusion/strip-css-styles
Update: well I thought the question was simpler. If you want to see my original answer just see the edit history for this post. This answer uses a similar principle though where you search for an item that you don't want removed and then push it back in with the replace.
<cfset teststr1 = "font-family: BebasNeue;" />
<cfset teststr2 = "font-family: Verdana" />
<cfset teststr3 = "font-family: BebasNeue; font-family: Times New Roman; color: red" />
<cfset search1 = "(font-family:\s*)((BebasNeue)|[\w ]+)(;)?" />
<cfset replace1 = "font-family: \3swiss7\4" />
<cfset search2 = "BebasNeueswiss7" />
<cfset replace2 = "BebasNeue" />
<cfoutput>
<ol>
<li>#replaceNoCase(reReplaceNoCase(teststr1, search1, replace1, "all"), search2, replace2, "all")#</li>
<li>#replaceNoCase(reReplaceNoCase(teststr2, search1, replace1, "all"), search2, replace2, "all")#</li>
<li>#replaceNoCase(reReplaceNoCase(teststr3, search1, replace1, "all"), search2, replace2, "all")#</li>
</ol>
</cfoutput>
Result:
1. font-family: BebasNeue;
2. font-family: swiss7
3. font-family: BebasNeue; font-family: swiss7; color: red
So essentially you replace all font families with the chosen font type, in this case swiss7, but by including the group selector in the replace you leave the BebasNeue font in the string. An additional step then cleans up the combined font name left behind.
I would suggest a more maintainable approach:
if !findNoCase( styleString, 'BebasNeue'){
styleString = REReplace(style, 'font-family:[^"|^;]*', "font-family:swiss7; font-size:12", "ALL");
}
This should get you want you are after: leave it alone if it is BebasNeue, otherwise, change the font-family to swiss7 and add a font-size. But it is also not so complicated that a regex-newbie couldn't understand what is going on.
<cfoutput>
<cfsavecontent variable="s">
This is some text. It is true that Harry Potter is a good
</cfsavecontent>
<cfset matches = reMatch("<[aA].*?>",s) />
#matches#
</cfoutput>
I need to get only "http://www.cnn.com" how to do this
#Bhargavi : Your regex is fine.
Try #matches[1]# instead.
<cfdump var="#matches#"> will show you the array of values on which the results can be manipulated accordingly.
New to coldfusion, new to regex...
I have a directory of files, named with "some" followed by a 13digit number, followed by underscore, ID and file ending like so:
some0000000000000_ID.jpg
ID can be any string.
How would I get the ID using regex? I guess I'd be looking for something like this, which captures everything between the underscore and file ending dot:
_\A[A-Z]*[a-z]*[0-9]*$
but I'm really not getting anywhere. Can someone point me in the right direction?
Thanks!
EDIT:
I ended up doing it like this, which is hack-ish but works nicely:
<cfset cropFront = #ListRest(ReReplaceNoCase(name, ".png|.jpg", ""), "_")#>
<cfset cropFull = #ListFirst(ReReplaceNoCase( cropFront, "xxxxx", ""), "." )#>
Maybe useful for someone else, too!
<cfdirectory name="images" directory="#path#" filter="some?????????????_ID.jpg">
The filter is not a regex pattern. It only knows the ? and * wildcard characters.
Can't test at the moment but this is the idea...
<cfdirectory name="files" directory="path" action="list" />
<cfloop query="files">
<cfset findinfo = refind("^some(\d{13})_", files.name, 0, true) />
<cfif arraylen(findinfo.pos) eq 2>
<cfset fileid = mid(files.name, findinfo.pos[2], findinfo.len[2]) />
<!--- do something --->
</cfif>
</cfloop>