Regular Expression (Regex) as assignment activity
I want to do something like this in an Assign activity:
Regex rgx = new Regex("[^0-9]"); str = rgx.Replace(str, "");
Where str is my new string that will be put into a new database field (the PhoneNumber field).
Basically I have phone numbers that can be entered in a Display Form as (xxx) yyy-zzzz or xxx-yyy-zzzz or xxx.yyy.zzzz. What I want to have go into the database record is xxxyyyzzzz, just to keep things consistent. I figured I would use a regular expression to strip out any non-numeric characters and leave me with what I want. I, unfortunately, do not know how to do this in the workflow designer. Any suggestions or examples out there?
I am imagining that I set the variable rgx with the Variable type of System.ComponentModel.DataAnnotations.RegularExpressionAttribute. Is this correct? What do I put in the Assign activity? Do I need to set the Default for rgx to something like new RegularExpression("[^0-9]"). Sorry, I'm a newbie to Regular Expressions and Workflow ;).
Thanks,
Michael
P.S. Maybe I should try to force the user, in the Display Form, to use one entry type format only (instead of the three listed) to make this easier? I have used the following Regex validation in the Validation part of the form: ^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$ (the one supplied in the Workflow Designer in the Validation Designer for Phone Number).
-
Hi Michael,
I've put together a sample workflow that strips non-numeric characters out of the input. You can download it (https://data.latitudegeo.com/community/regex.zip) here .
It basically does exactly what you've described, except uses System.Text.RegularExpressions.Regex instead of System.ComponentModel.DataAnnotations.RegularExpressionAttribute.
The Form allows the user to enter any text. Next comes the first Assign statement, which sets the System.Text.RegularExpressions.Regex variable rgx to new System.Text.RegularExpressions.Regex("[^0-9]"). The next Assign statement applies the Regex to the string that was entered, and the resulting series of numbers are displayed in an Alert.
-Victoria
0 -
Thanks Victoria,
That was all the help I needed. Awesome!
Michael
0 -
I tried to open the workflow and I got an error saying that the workflow could not be loaded or was not a valid workflow.
Any suggestions?
Robert
0 -
Robert,
When I download the workflow and try to open it using the "Open with" method after double-clicking on the xaml file, it seems to fail. But there is another method...I would suggest downloading the zip file, then extracting the .xaml from this zip to a known location, then open Workflow Manager and use the File-->Open from the dropdowns in Workflow Manager, then navigate to the file and open it that way. That method worked for me. Try again using this method and you should be able to open it.
I hope that makes sense,
Michael
0 -
Hi Robert,
Microsoft has changed so underlying behavior on WPF applications (which Workflow Designer is one.) If you download a workflow file, you'll need to right-click on the file, open the properties, look for the Unblock button and click it. The file will then load in Workflow Designer.
regards,
Edmond0 -
This would be useful, but the zip file seems to be unawailable now? 0 -
Sorry about that! I've fixed (https://data.latitudegeo.com/community/regex.zip) the link to Victoria's workflow. 0
Bitte melden Sie sich an, um einen Kommentar zu hinterlassen.
Kommentare
7 Kommentare