Coldfusion ReReplace finding numbers and replacing with a new number - coldfusion

Sorry but I am very new to Coldfusion and I need some help please.
I have a string that contains a recipe method. "Heat your oven to 200c and then blah blah until internal temperature measures 60c"
I need to replace the numeric values in the string with a dynamically calculated value and then it will look like this: "Heat your oven to 200c (392f) and then blah blah until internal temperature measures 60c (140f)".
These numeric values can appear anywhere and multiple times in the string.
The calculation to convert from C to F is C * 9/5 + 32.
So I need to ReReplace all numbers in the string with a new value calculated dynamically.
I hope this is clear.

This is my method of doing it.
<cfscript>
sString = "Heat your oven to 200c and then cook till the temp internally is 60c";
aFind = ReMatchNoCase("\d+c",sString);
x = 0;
while(x < arrayLen(aFind)){
x++;
nCalc = RematchNoCase("\d+",aFind[x])[1];
nCalc = nCalc*9/5+32;
sString = ReReplaceNoCase(sString,aFind[x],'#aFind[x]# (#ncalc#F)');
}
writeOutput(sString);
</cfscript>
Adams linked UDF might be better, but I guess this code example, serves as a look into how you might accomplish it without a 3rd party.

Related

Input Formatter Regex for simple mathematical calculation in Flutter/Dart

I want to ensure valid input on my text field where user can key in simple math expression for numbers up to 2 d.p such as "1.10 + 3.21 x 0.07". I am doing this through the adding regex to the input formatter constructor in the TextField class.
Following the regex example for 2 d.p here, I modified the code for my text field input formatter to include operators:
String dp = (decimalRange != null && decimalRange > 0)
? "([.][0-9]{0,$decimalRange}){0,1}"
: "";
String num = "($dp)|([0-9]{1,4}$dp)";
_exp = new RegExp(
"^($num){0,1}[-+x/]{0,1}($num){0,1}[-+x/]{0,1}($num){0,1}\$");
I am able to achieve "1.10 + 3.21 x 0.07", however, the user can also type invalid value into the textfield such as "1...10", "1.10 + 3..21". Any advice to improve the Regex above would be greatly appreciated!
Note that I also limit the user to key in a maximum of 3 decimal numbers. so "(2d.p)(operator)(2d.p)(operator)(2d.p)is the maximum limit.

Change characters at specific position in a string in SAS

IS there a function to change letters at a given index in SAS?
For example if my string is
string1 = 'abcd1234efgh'
I want to do somehing like:
string2 = somefunction(string1, 5, 'zzzz');
to produce
'abcdzzzzefgh'
Yes, substr() = is what you're looking for. See here for details.
substr(string2, 5) = 'zzzz';
The substr(variable,position<,length>) = function can also take an third argument to define the length of the segment to be replaced.

Excel VBA Macro: Using regex to return adjacent row data

Alright; so here's the whole thing I'm suppose to do.
Input a number that corresponds with a number in Data Worksheet Column A and return the adjacent row data.
I want it to return the adjacent cells; example. If it finds 052035 in cell A5378, I Want it to return the data or cell numbers B5378, C5378
EDIT: I've deleted my code; since it didn't really follow with a good way to do it.
Worksheet Structure for Data:
A 1-7800ish[6 Digit number 1-9]
B 1-7800ish Area Codes
C 1-7800ish City/States
The data by the way; is a relatively large set that I got from a query on a SQL-Server. The string number that I'm looking for should have no duplicates based on my original query. [I grouped by before copying it over]
If ya'll have resources for a quick introduction to VB from a programming perspective that'll be helpful. I can program in C/C++ but the syntax in VB is a little weird to me.
If your end goal is to simply find the exact match in column A, and return the values in corresponding row, columns B & C, Regular Expressions is the wrong tool for the job. Use built in functions like Match.
I still don't understand the point of this exercise, as, the data is already arranged in columns A, B and C., you could simply use AutoFilter... This subroutine simply tells you that the value is found (and returns the corresponding data) or not found.
I have tested this (made a small change in dimensioning vals variable)
Sub Foo()
Dim valToLookFor As String
Dim rngToLookAt As Range
Dim foundRow As Long
Dim vals() As Variant
valToLookFor = "052035"
Set rngToLookAt = Range("A:A")
If Not IsError(Application.Match(valToLookFor, rngToLookAt, False)) Then
foundRow = Application.Match(valToLookFor, rngToLookAt, False)
ReDim vals(1)
vals(0) = rngToLookAt.Cells(foundRow).Offset(0, 1).Value
vals(1) = rngToLookAt.Cells(foundRow).Offset(0, 2).Value
'Alternatively, to return the cell address:
'vals(0) = rngToLookAt.Cells(foundRow).Offset(0,1).Address
'vals(1) = rngToLookAt.Cells(foundRow).Offset(0,2).Address
MsgBox Join(vals, ",")
Else:
Erase vals
MsgBox valToLookFor & " not found!", vbInformation
End If
End Sub
Here is proof that it works:

How to separate a line of input into multiple variables?

I have a file that contains rows and columns of information like:
104857 Big Screen TV 567.95
573823 Blender 45.25
I need to parse this information into three separate items, a string containing the identification number on the left, a string containing the item name, and a double variable containing the price. The information is always found in the same columns, i.e. in the same order.
I am having trouble accomplishing this. Even when not reading from the file and just using a sample string, my attempt just outputs a jumbled mess:
string input = "104857 Big Screen TV 567.95";
string tempone = "";
string temptwo = input.substr(0,1);
tempone += temptwo;
for(int i=1 ; temptwo != " " && i < input.length() ; i++)
{
temptwo = input.substr(j,j);
tempone += temp2;
}
cout << tempone;
I've tried tweaking the above code for quite some time, but no luck, and I can't think of any other way to do it at the moment.
You can find the first space and the last space using std::find_first_of and std::find_last_of . You can use this to better split the string into 3 - first space comes after the first variable and the last space comes before the third variable, everything in between is the second variable.
How about following pseudocode:
string input = "104857 Big Screen TV 567.95";
string[] parsed_output = input.split(" "); // split input string with 'space' as delimiter
// parsed_output[0] = 104857
// parsed_output[1] = Big
// parsed_output[2] = Screen
// parsed_output[3] = TV
// parsed_output[4] = 567.95
int id = stringToInt(parsed_output[0]);
string product = concat(parsed_output[1], parsed_output[2], ... ,parsed_output[length-2]);
double price = stringToDouble(parsed_output[length-1]);
I hope, that's clear.
Well try breaking down the files components:
you know a number always comes first, and we also know a number has no white spaces.
The string following the number CAN have whitespaces, but won't contain any numbers(i would assume)
After this title, you're going to have more numbers(with no whitespaces)
from these components, you can deduce:
grabbing the first number is as simple as reading in using the filestream <<.
getting the string requires you to check until you reach a number, grabbing one character at a time and inserting that into a string. the last number is just like the first, using the filestream <<
This seems like homework so i'll let you put the rest together.
I would try a regular expression, something along these lines:
^([0-9]+)\s+(.+)\s+([0-9]+\.[0-9]+)$
I am not very good at regex syntax, but ([0-9]+) corresponds to a sequence of digits (this is the id), ([0-9]+\.[0-9]+) is the floating point number (price) and (.+) is the string that is separated from the two number by sequences of "space" characters: \s+.
The next step would be to check if you need this to work with prices like ".50" or "10".

Matching algorithm or regular expression?

I have a huge log file with different types of string rows, and I need to extract data in a "smart" way from these.
Sample snippet:
2011-03-05 node32_three INFO stack trace, at empty string asfa 11120023
--- - MON 23 02 2011 ERROR stack trace NONE      
For instance, what is the best way to extract the date from each row, independent of date format?
You could make a regex for different formats like so:
(fmt1)|(fmt2)|....
Where fmt1, fmt2 etc are the individual regexes, for yor example
(20\d\d-[01]\d-[0123]\d)|((?MON|TUE|WED|THU|FRI|SAT|SUN) [0123]\d [01]\d 20\d\d)
Note that to prevent the chance to match arbitrary numbers I restricted year, month and day numbers accordingly. For example, a day number cannot start with 4, neither can a month number start with 2.
This gives the following pseudo code:
// remember that you need to double each backslash when writing the
// pattern in string form
Pattern p = Pattern.compile("..."); // compile once and for all
String s;
for each line
s = current input line;
Matcher m = p.matcher(s);
if (m.find()) {
String d = m.group(); // d is the string that matched
....
}
Each individual date pattern is written in () to make it possible to find out what format we had, like so:
int fmt = 0;
// each (fmt) is a group, numbered starting with 1 from left to right
for (int i = 1; fmt == 0 && i <= total number of different formats; i++)
if (m.group(i) != null) fmt = i;
For this to work, inner (regex) groups must be written (?regex) so that they do not count as capture-groups, look at updated example.
If you use Java, you may want to have a look at Joda time. Also, read this question and related answers. I think Joda DateTimeFormat should give you all the flexibility that you need to parse the various date/time format of your log file.
A quick example:
String dateString = "2011-04-18 10:41:33";
DateTimeFormatter formatter =
DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
DateTime dateTime = formatter.parseDateTime(dateString);
Just define a String[] for the formats of you date/time, and pass each element to DateTimeFormat to get the corresponding DateTimeFormatter. You can use regex just separate date strings from other stuff in the log lines, and then you can use the various DateTimeFormatters to try and parse them.