?
Solved

RegEx how to

Posted on 2005-02-28
25
Medium Priority
?
368 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month12 days, 10 hours left to enroll

777 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