How to implement this regular expression in ColdFusion (or Java)?

I found this very convenient regex on regexlib.com, but I don’t understand how to implement it in my application.

(?:(?:(?<Feet>\d+)[ ]*(?:'|ft)){0,1}[ ]*(?<Inches>\d*(?![/\w])){0,1}(?:[ ,\-]){0,1}(?<Fraction>(?<FracNum>\d*)\/(?<FracDem>\d*)){0,1}(?<Decimal>\.\d*){0,1}(?:\x22| in))|(?:(?<Feet>\d+)[ ]*(?:'|ft)[ ]*){1} 

I tested it with my online testing tool and it really does everything I need. Inserting it as a ColdFusion REFind () parameter did not predictably work and returned a useless error message.

I work in ColdFusion, so I have access to Java classes if necessary. Answers in Java or ColdFusion will be helpful.

A good example line would be something like this: 5 '1/2 "

EDIT

I need to use groups in a regex to extract data, and not just use it to validate a string. Think this means I should use REMatch ()? Please excuse my lack of regex experience!

EDIT 2

It seems that using REFind () with this expression:

 (?:(?:(\\d+)[ ]*(?:'|ft)){0,1}[ ]*(\\d*(?![/\\w])){0,1}(?:[ ,\\-]){0,1}((\\d*)\\/(\\d*)){0,1}(\\.\\d*){0,1}(?:\\x22| in))|(?:(\\d+)[ ]*(?:'|ft)[ ]*){1} 

does not find matches for most of the test data I give them, including those that return matches using the regexlib.com tester: 1ft 2-3/4 in, 2' 3 4/5", 3ft, 4', 5 in, 6", 7.125 in, 3ft 4.5 in

+4
source share
3 answers

. <foo> syntax is not supported in ColdFusion. I am not familiar with this syntax, but it seems like it is used to assign names to captured subexpressions. For example, the first subexpression is a number representing the legs, so it has the value <Feet> tag. You can remove those tags without affecting the regular expression match.

I have not tested it, but all the other elements that I see in this regular expression are supported in ColdFusion, so REFind () should work after removing all the <foo> tags. Access to subexpressions is, of course, supported by the "Return Expressions" argument. See Standard CF Documents at REFind ().

Aside, the regex seems a bit verbose. {0,1} is rare, how? means the same thing. {1} even less often, since it is by default for groupings and, therefore, can be completely omitted.

ADDITION

 regex = "(?:(?:(\\d+)[ ]*(?:'|ft)){0,1}[ ]*(\\d*(?![/\\w])){0,1}(?:[ ,\\-]){0,1}((\\d*)\\/(\\d*)){0,1}(\\.\\d*){0,1}(?:\\x22| in))|(?:(\\d+)[ ]*(?:'|ft)[ ]*){1}"; subs = REFind(regex,input,1,"True"); if (subs.pos[1] eq 0) { found = "False"; } else { found = "True"; feet = Mid(input,subs.pos[2],subs.len[2]); inches = Mid(input,subs.pos[3],subs.len[3]); fraction = Mid(input,subs.pos[4],subs.len[4]); fracNum = Mid(input,subs.pos[5],subs.len[5]); fracDem = Mid(input,subs.pos[6],subs.len[6]); decimal = Mid(input,subs.pos[7],subs.len[7]); if (feet is "") { // Use the _other_ feet feet = Mid(input,subs.pos[8],subs.len[8]); } } 
+4
source

It looks like the expression you had was in C # syntax that supports named groups (e.g. (?<Decimal>\.\d*) ). Java does not and treats them as something completely different. Since named groups are not used in any way, it is simply a matter of removing the naming (for example, (?<Decimal>\.\\d*) becomes (\.\\d*) ).

 Pattern.compile("(?:(?:(\\d+)[ ]*(?:'|ft)){0,1}[ ]*(\\d*(?![/\\w])){0,1}(?:[ ,\\-]){0,1}((\\d*)\\/(\\d*)){0,1}(\\.\\d*){0,1}(?:\\x22| in))|(?:(\\d+)[ ]*(?:'|ft)[ ]*){1}"); 
+1
source

Java String object supports regex's. Sting.match (), String.replaceAll () and String.replaceFirst ().

0
source

All Articles