Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

RegEx how to

Posted on 2005-02-28
25
Medium Priority
?
369 Views
Last Modified: 2010-04-16
Can someone provide me with RegEx code that will turn

THIS

Dim LenderPhone_Before As Variant


INTO THIS:

Write #1, "LenderPhone, " & LenderPhone_Before


public string ConvertVarIntoPhrase(string oldString)
{

}


==============

For example:

string oldString = "Dim LenderPhone_Before As Variant";
string newString="";

newString = ConvertVarIntoPhrase(oldString)



newString now contains:

Write #1, "LenderPhone, " & LenderPhone_Before


========================================


I will be working on this as well......as I am trying to learn RegEx.....
0
Comment
Question by:Tom Knowlton
  • 15
  • 10
25 Comments
 
LVL 58

Expert Comment

by:amit_g
ID: 13424053
Try this ...

    Dim reg_exp As New RegExp
    reg_exp.Pattern = "Dim (.*?)_(.*?) As Variant"
    reg_exp.IgnoreCase = True
    reg_exp.Global = True
    reg_exp.Replace(InString, "Write #1, ""$1,"" & $1_$2")
0
 
LVL 58

Expert Comment

by:amit_g
ID: 13424065
Oh that is VB.Net. Convert it to C#. The pattern should be

Dim (.*?)_(.*?) As Variant

and the replace string should be

Write #1, \"$1,\" & $1_$2

with " escaped with \
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 13424068
What would it be in C#?

Did I post in the wrong TA....or did you assume I wanted VB?   :)

Anyways....thanks!!!!   man you guys are fast!!!!  wow!
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 5

Author Comment

by:Tom Knowlton
ID: 13424075
Nevermind....you got it!
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 13424094
So the C# method internal code would look like......????

public string ConvertVarIntoPhrase(string oldString)
{
???????
????????
???????
}
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 13424118
So it is

using System.Text.RegularExpressions

that gives me access to the RegEx class library?
0
 
LVL 58

Expert Comment

by:amit_g
ID: 13424145
using System.Text.RegularExpressions

public string ConvertVarIntoPhrase(string oldString)
{
    string newString;
    RegEx reg_exp = new RegEx;

    reg_exp.Pattern = "Dim (.*?)_(.*?) As Variant";
    reg_exp.IgnoreCase = true;
    reg_exp.Global = true;

    newString = reg_exp.Replace(oldString, "Write #1, ""$1,"" & $1_$2");

    return newString;
}
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 13424176
Super!

Let me try it out......


Tom
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 13424414
using System;
using System.Text.RegularExpressions;


namespace ConsoleApplicationTestOOPConcepts
{
      /// <summary>
      /// Summary description for Class1.
      /// </summary>
      class ClassTestOOOP
      {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main(string[] args)
            {
                  string exp = "Dim LenderPhone_Before As Variant";
                  string newStr;
                  newStr = ConvertVarIntoPhrase(exp);
                  Console.WriteLine(newStr);
                  Console.ReadLine();
                  
                  //
                  // TODO: Add code to start application here
                  //

//                  Branch br = new Branch();
//                  //Branch br2 = new Branch();
//
//                  br.GetBranch();
//                  
//                  //br2 = (Branch)br.GetCopy();
//
//                  br.BranchName = "Tom";
//
//                  //br.CheckForChanges(br2);
//
//                  br.CheckForChanges();


//
//                  ClosingAgency ca = new ClosingAgency();
//
//                  ca.GetBranch();
//
//                  Console.WriteLine(ca.ClosingAgencyName);
//                  Console.WriteLine(ca.BranchName);            
            }



            static public string ConvertVarIntoPhrase(string oldString)
            {
                  string newString;
                  Regex reg_exp = new Regex("abc");

                  //reg_exp.Pattern = "Dim (.*?)_(.*?) As Variant";
                  //reg_exp.IgnoreCase = true;
                  //reg_exp.Global = true;

                  newString = reg_exp.Replace(oldString, "Write #1, \"$1,\" & $1_$2");

                  return newString;
            }
      }
}



When I return newString...........   newString equals oldString........   hmmmm........


The replace does not seem to be working.
0
 
LVL 58

Expert Comment

by:amit_g
ID: 13424475
A working example ...

        private void TestRegEx()
        {
            string oldString, newString;
            Regex reg_exp = new Regex("Dim (.*?)_(.*?) As Variant");

            oldString = "Dim LenderPhone_Before As Variant";

            newString = reg_exp.Replace(oldString, "Write #1, \"$1,\" & $1_$2");

            Trace.Warn(oldString);
            Trace.Warn(newString);
        }
0
 
LVL 58

Accepted Solution

by:
amit_g earned 2000 total points
ID: 13424484
Everything else seems ok. All you need to change is

Regex reg_exp = new Regex("abc");

to

Regex reg_exp = new Regex("Dim (.*?)_(.*?) As Variant");
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 13424527
FOR MY REFERENCE:

static public string ConvertVarIntoPhrase(string oldString)
{
      string newString;
      Regex reg_exp = new Regex("Dim (.*?)_(.*?) As Variant");
      newString = reg_exp.Replace(oldString, "Write #1, \"$1,\" & $1_$2");
      return newString;
}
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 13424744
I am trying to dissect the following.

I have a book from O'Reilly on "Mastering Regular Expressions"

(.*?)


(    )   is an alternation sequence    ????


.      means  "any one character"

*     means   "any number, including none, of the item"      item being   "any one character"

?      means  preceding character is allowed to appear at this point in the expression, but whose existence is not required  to still be considered a successful match.


================


So taken as a whole.......this

Regex("Dim (.*?)_(.*?) As Variant");

seems to say

"The string we are creating variables for MUST start with the following four characters:  Dim<space>, then there can exist ANY number of characters or no characters, then a   "_"  ,  then there can exist ANY number of characters or no characters, then the following sequence of characters:  <space>As<space>Variant

0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 13424752
Each  (.*?)      means    one    "variable"     ?????

the first   (.*?)  =  $1


the second   (.*?)  =  $2


?????

0
 
LVL 58

Expert Comment

by:amit_g
ID: 13424777
You got most of it. Anything inside () can be refernced later in the replace using $1, $2 and so on.

.* is any character, any number of times.
.*? is any character, any number of times but consume as less characters as possible. So this stops at the next pattern item.

Without a ? .* will match all the way to the end. With ? .*? matches upto next item (_).

0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 13424785
>>>>Without a ? .* will match all the way to the end. With ? .*? matches upto next item (_).


can you give me a few more examples?
0
 
LVL 58

Expert Comment

by:amit_g
ID: 13424795
.* matches any character any number of times. So there is no stopping. So in

Regex("Dim (.*)_(.*) As Variant");

the first .* would have matched

LenderPhone_Before As Variant

but we wanted it to stop at _. That is acomplished using ?.
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 13424806
In effect:

?    means   "keep going any number of times until you reach the first character outside the (...)  alternation sequence"

 ???????
0
 
LVL 58

Expert Comment

by:amit_g
ID: 13424829
( ) has nothing to do with it. () is used to capture a matched patter in positional variables ($1, $2...). If we had this

Regex("Dim .*_.* As Variant");

the stopping happens at _. Although in this case it doesn't really matter as we are not capturing the matches.

.* is hungry. It will eat up all available characters and stop only at the end. ? stops that behavior and let it consume as little as possible so that the remaining pattern ("_.* As Variant" in this case) can be applied.
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 13424853
?      tells     .*     "eat as little as you can until you reach the _ "

??
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 13424867
So    the    (    )            is there simply to say    "setup a new variable for each sequence of letters that mach   .*?"


In our case.....two variables got created.
0
 
LVL 58

Expert Comment

by:amit_g
ID: 13424898
>> ?      tells     .*     "eat as little as you can until you reach the _ "

Yes.

>> So    the    (    )            is there simply to say    "setup a new variable for each sequence of letters that mach   .*?"

Yes.
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 13424905
LOL......Tom finally got it.


RegEx is hard........but seems worth the effort.......
0
 
LVL 58

Expert Comment

by:amit_g
ID: 13424923
Yes it is hard but it can do miracle in string manipulations.
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 13424930
Yes....the O'Reilly book I bought promises me that it is very powerful as far as text manipulation.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
Suggested Courses
Course of the Month11 days, 20 hours left to enroll

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question