I am trying to get the value of the first letter in my array. For some reason its just chopping off my first letter. Will someone please tell me where I am going wrong?
<cfset MidInitial = "Hugh" />
<cfset MidInitArray = ReMatch("[a-z]",MidInitial) />
<cfdump var="#MidInitArray#" />
Array Hugh
[1] u
[2] g
[3] h
Where is the H going?!
ReMatch is case sensitive.
Either use (?i) infront of your regex or add [A-Za-z] in your regex.
Do you just want the first character in the string? If so, you don't need regex for this and a Left(MidInitial,1) would work.
Related
Using ColdFusion, how can I get the "my link alias" part out of the string below? It will always use the "alias" attribute, in case that helps.
learn more
In general, don't use regex use jsoup (or another HTML parser) - it's basically:
<cfset Alias = jsoup.parse(input).select('a[alias]').attr('alias') />
For more info, Henry posted a link to a blog post by Ben Nadel, there's also a blog post by Ray Camden, and if you search on SO you'll find plenty of jSoup/CF questions with useful info in them.
If you're only dealing with a string in the format you provided (i.e. just an A tag, not a whole chunk of HTML), you can potentially do it as simple as:
<cfset Input = 'learn more' />
<cfset Parts = Input.split('\salias="',2) />
<cfset Alias = ListFirst(Parts[2],'"') />
Or to deal with possible spaces and single or double quotes:
<cfset Input = 'learn more' />
<cfset Parts = Input.split('\salias\s*=\s*(?=["''])',2) />
<cfset Alias = ListFirst( Parts[2] , Left(Parts[2],1) ) />
But if you're in any way unsure about what the markup will be, use a robust HTML parser instead.
Description
You could use this regex which will require the a tag, and won't accidentally match any of the other tags which start with a. It will also allow either double or single quotes surrounding the attribute's value. The value of the positive lookahead here is probably lost on this particular question because we're only checking a single attribute, if you're looking to capture multiple attributes then multiple lookaheads can be used to capture the attributes in any order.
<a\b(?=[^>]*\s\balias=(["'])((?:(?!\1)|.)*)\1)[^>]*>(.*?)<\/a>
Groups
Group 0 gets the entire string from open anchor tag through the close tag
gets the type of quote used so it be correctly matched when closed
gets the value of the alias attribute
gets the value between the open and close anchor tags. This covers a possible intent of the original unedited question from the requester before the question was edited.
Example Match
Given input text: learn more
[0] => learn more
[1] => "
[2] => my link's alias
[3] => learn more
I am coding for Email validation which may take set of special characters. I could successfully add others to RegEx. However when I try for '+' and '%' it gives me an error.
I used '\' to allow special characters.
\+ --> adds a space removing + sign
\% --> removes 3rd char after % sign
ColdFusion has several built-in validation functions for things such as email addresses. You could simply use something like:
<cfif IsValid("email", YourEmailVar)>
<!--- do what you want for success here --->
<cfelse>
<!--- do what you want for validation failure here --->
</cfif>
Documentation for IsValid function
The IsValid function will also allow you to use RegEx if you prefer.
EDIT
In order to validate variables from the URL scope simply prepend that to the variable name. Like so:
<cfif IsValid("email", URL.YourURLEmailVar)>
I have a simple regex line to extract the src="" value from an image tag:
<cfset variables.attrSrc = REMatch("(?i)src\s*=\s*""[^""]+", variables.myImageTag) />
<!--- REMatch("(?i)src\s*=\s*""[^""]+" --->
However, while this works great, it doesn't appear to be working with src='' attrubutes that display single quotes instead of double.
Ideally, I'd like it to work with both single quotes and double.
Any thoughts?
Thanks,
Michael.
(?i)src\s*=\s*(""[^""]+""|'[^']+')
Is there a way to write in coldfusion something like:
< cfset ReReplace(value,"&#\d+;","#decodeHtmlEntity(\1)#", "all") >
Thanks a lot
The short answer is "No".
CF doesn't handle the regular expression execution natively. It hands off to a Java library (Oro, IIRC) to handle that. This means that any CF functions you call get executed before toe regex.
There is a workaround, although it's not nearly as elegant as being able to pass functions would be. Use reFind() to discover all the instances of what you are looking for, and repolace them one-by-one. If you do the replaces last-to-first (eg if there are 3 instances, do the 3rd, then the 2nd, then the 1st) your starting point for each match will remain in the same location, so you can do an reFind all, instead of doing the reFind in the loop.
HTH.
I don't think this will work if you want to replace regular expression value as argument of decodeHTMLEntity.
Updated:
<cfset myVar = ReReplace("ABC123DEF","(\d+)",addOne('\1'), "all") >
<cffunction name="addOne" access="public" output="false" returntype="string">
<cfargument name="arg1" required="true" type="string" />
<cfreturn arg1 + 1>
</cffunction>
<cfdump var="#myvar#">
Above code written to find 123 from text and add one into it but this will not work as arg1 will have \1 which is not numeric value.
Have you tried simply using URLDecode(value)?
Or if you specifically only want to decode the numeric html codes, then
<cfset myVar = ReReplace(value,"(&##[\d+];)",urlDecode('\1'), "all") >
will do what you need.
To explain what it is doing :
I've replaced the PHP decodeHTMLEntity function with the CFML version.
If you want to use back references you need to specify the capture groups in the regex pattern.
you need to double up those #'s to escape them, otherwise CF will be looking for a close # that it will never find.
I currently have code to remove html from a string:
<cfset arguments.textToFormat = REReplaceNoCase(arguments.textToFormat,"<[^>]*>","","ALL") />
However, this does not remove html characters like
What Regex could I use to ReReplace these characters??
Thanks
For removing and other similar strings :
&[^;]+?;
HTH