Coldfusion - Twitter style mentions to link records - regex

was wondering if anyone could point me in the right direction on this one.
The system I'm building allows users to add comments to records. If the user wants to reference another record in their comment I want them to be able to use the # symbol followed by the 5 digit record id.
For example when a user submits a comment like "Updated details for this record and record #25645" I need it to be outputted with a href around the "#25645" which will link to the record in question.
I'm trying to use REReplaceNoCase with limited success.
<cfset LinkableComments = REReplaceNoCase(Comments, "#[0-9][0-9][0-9][0-9][0-9]", "Test", "all") />
I can't figure out how to get the records id back into the URL's.
Any thoughts?

Leigh is correct, you can use a back reference like so:
<cfset LinkableComments = REReplaceNoCase(Comments, "#([0-9][0-9][0-9][0-9][0-9])", "#\1", "all") />
Basically you place the regex "group" you wish to capture and replace inside ( and ), then refer to that in the replace with argument as \1 ( because this was the first group, \2 for second, etc... )
Try it yourself here ( <-- Edit: Gist was updated to show the #([0-9]{5}) syntax suggested by #PeterBoughton)

According to the documentation on REReplace, you ought to be able to use the following:
REReplaceNoCase(Comments, "#([0-9]{5})", "Test", "all")
I'll test when my CF server is back up again

Related

How can I display extra details in the autosuggest, but only ID in the input field?

I'm using a ColdFusion autosuggest for my UserID field. When a user starts to type in a UserID, a list of user ids and the user names associated with it pop up (populated by a cfc function). Sample code:
<cfinput name="userID" type="text" value="#userID#" autoSuggest="cfc:Utilities.suggestUser({cfautosuggestvalue})" matchcontains="true" />
The suggestions are listed in the format "User Name <userID>". So if the user would start typing 123, a sample suggestion that would pop up would be "Harvey Mann <1234>".
The problem is that if the user chooses that suggestion, I don't want to insert the whole suggested text into the input field - I just want to insert the user id ("1234" in this case). I would also like to be able to insert the user name ("Harvey Mann") into an adjacent field/area if possible. Is there any way to accomplish this?
Since you are using CF's built-in implementation of autosuggest, you are stuck with only having access to one returned value. Thus if the value consists of mutliple 'parts' and you want to insert different 'parts' of the value into different columns, you will have to parse the value and extract appropriate parts from the string. In your example case you could treat the value as a list delimited by <. In this case you can get the name 'part' with
trim(listfirst(form.userID, "<"))
and the id 'part' with
replace(listlast(form.userID, "<"), ">", "")
Alternatively, you can always use jQuery UI Autocomplete implementation - you'll have to write your own javascript for it, but it gives you far more control than the built-in cf's implementation. Check jQuery UI docs for more info: http://jqueryui.com/demos/autocomplete/
UPDATE:
On second thought, if you just want to display selected value's 'name' part in another area on the same page, you may be able to do it using just CF's built-in autosuggest and a bit of javascript. Try adding this code to the HEAD section of your page:
<cfajaxproxy bind="javaScript:showName({userID})">
<script type="text/javascript">
showName = function(strUserId) {
document.getElementById("someID").innerHTML = strUserId.split('<')[0];
}
</script>

Getting the website title from a link in a string

string: "Here is the badges, https://stackoverflow.com/badges bla bla bla"
If string contatins a link (see above) I want to parse the website title of that link.
It should return : Badges - Stack Overflow.
How can i do that?
Thanks.
#!/usr/bin/perl -w
require LWP::UserAgent;
my $ua = LWP::UserAgent->new;
$ua->timeout(10);
$ua->env_proxy;
my $response = $ua->get('http://search.cpan.org/');
if ($response->is_success) {
print $response->title();
}
else {
die $response->status_line;
}
See LWP::UserAgent. Cheers :-)
I use URI::Find::Simple's list_uris method and URI::Title for this.
Depending how the link is given and how you define title, you need one or other approach.
In the exact scenario that you have presented, getting the URL with URI::Find, HTML::LinkExtractor etc, and then my $title=URI->new($link)->path() will provide the title and the link.
But if the website title is the linked text like badged, then How can I extract URL and link text from HTML in Perl? will give you the answer.
If the title is encoded in the link itself and the link is the text itself of the link, how do you define the title?
Do you want the last bit of the URI before any query? What happens with the queries set as URL paths?
Do you want the part between the host and the query?
Do you want to parse the link source and retrieve the title tag if any?
As always going from trivial first implementation to cover all corner cases is a daunting tasks ;-)

ColdFusion Search

I’ve created a file index of all of my ColdFusion files so I can quickly search the files and find what I’m looking for. So far, it’s working great except it doesn’t seem to be searching inside any ColdFusion tags.
For example…
<p>If I searched for this text, It would return a result</p>
<cfset variables.foo = "however, If I search for this text it wouldn’t return any results." />
Does anyone know if there’s a way to search inside of a ColdFusion tag like that?
This is my index..
<cfindex
collection = "fileIndex"
action="refresh"
type="path"
key="d:\my-websites-location\"
urlpath="http://mywebsite/"
extensions=".cfm, .cfml, .cfc"
recurse="Yes">
This is my search…
<cfsearch
name = "testSearch"
collection = "fileIndex"
type="internet"
criteria = "variables.foo"
/>
Any ideas?
Thanks,
Paul : )
It looks like the type="internet" may be your issue. Try removing the "type" attribute and see what you get.
Use a query that does get the record and look at the "summary" field of your result. I suspect the markup is being stripped.
On ColdFusion 9, with solr it doesn't index the markup, however Verity does. A workaround you could use a combination of cffile/cfdirectory to read each file one by one and feed it into the collection. This will preserve the markup and make it searchable.
Or you can enclose your criteria variable with ##.
<cfsearch
name = "testSearch"
collection = "fileIndex"
type="internet"
criteria = "#variables.foo#"
/>

Regular Expression Match for Google Analytics Goal Tracking

I have a coupon request form on every page on my website, when the coupon form is submitted you are taken to the same page you are on with an additional "?coupon=sent" parameter added to the query string. I would like to be able to track any page url wiht ?coupon=sent on the end as a goal. Currently, I have this:
/[^.][.php][\?coupon\=sent]+
which does not seem to be doing the trick. Any ideas?
Use this one:
/\?coupon=sent/

Parse request URL in JSTL

I want to display a specific message based on the URL request on a JSP.
the request URL can be:
/app/cars/{id}
OR
/app/people/{id}
On my messages.properties I've got:
events.action.cars=My car {0} event
events.action.people=My person {1} event
Finally, on my JSP page I want to have the following code:
<spring:message code="events.${element.cause}.${?????}"
arguments="${element.param['0']},${element.param['1']}"/>
I need help figuring out which expression I could use to parse the request URL and obtain the word before the ID.
You can access the request URI in JSTL (actually: EL) as follows:
${pageContext.request.requestURI}
(which thus returns HttpServletRequest#getRequestURI())
Then, to determine it, you'll have to play a bit round with JSTL functions taglib. It offers several string manipulation methods like split(), indexOf(), substringAfter(), etc. No, no one supports regex. Just parse it.
Kickoff example:
<c:set var="pathinfo" value="${fn:split(pageContext.request.requestURI, '/')}" />
<c:set var="id" value="${pathinfo[pathinfo.length - 1]}" />
And use it as ${id}.
/app/(cars|people)/([^/]*)$
will put cars or people in backreference \1, depending on the match, and whatever is left right of the last slash in backreference \2.
My solution so far is to have a RequestUtils class that match the regex ".?/jsp/(\w+)/..jsp" and return the group(1).
in my Jsp I got:
<% request.setAttribute("entity", RequestUtils.getEntityURI(request)); %>
<spring:message code="events.${element.cause}.${entity}"
arguments="${element.param['0']},${element.param['1']}"/>
this of course did the trick. But still it would be better not to have any Java code within the JSP.
If I understand you correctly, I think you need to do something like this:
#RequestMapping(value="/owners/{ownerId}", method=RequestMethod.GET)
public String findOwner(#PathVariable String ownerId, Model model) {
model.addAttribute("ownerId", ownerId);
return "myview";
}
As you can see, here the ownerId is read from the URL by Spring MVC. After that, you simply put the variable in the Model map so you can use it in your JSP.