Solved

Update Regx expression and Function

Posted on 2007-03-21
3
156 Views
Last Modified: 2010-04-23
VB.NET 2003
REGEX AND  Function need Updating:


I'am having problems with the following function below...
It is taking  normal 2 numerator and 2 denominator fractions and parsing them apart...


It should do the following:


11/2   1 1/2
11/4   1 1/4
11/8   1 1/8
21/2   2 1/2
21/4   2 1/4
21/8   2 1/8
31/2   3 1/2
31/4   3 1/4
31/8   3 1/8

or



11/16   1 1/16
21/64   2 1/64
21/32   2 1/32


it needs to leave alone double numerator and denominator
13/64
11/16
etc....


An assumption is a <space> before AND after the fraction




' regular fraction replace function
                ' dbRow.Item("fldDescription") = Regex.Replace(dbRow.Item("fldDescription"), _
                '   "(\d{2,}/\d|\d+\s+\d/\d+)", AddressOf StrFractionReplace)
                ' ===============================================
Private Function StrReplace(ByVal m As Match) As String

        Dim str As String = m.Value
        Dim strRep As String = ""

        If str.IndexOf(" "c) >= 0 Then
            Dim idx As Integer = str.IndexOf(" "c)
            strRep = str.Substring(0, idx) & "-" & str.Substring(idx + 1)
        Else
            Dim idx As Integer = str.IndexOf("/"c)
            strRep = str.Substring(0, idx - 1) & " " & str.Substring(idx - 1)
        End If

        Return strRep

    End Function


Thanks
fordraiders
0
Comment
Question by:fordraiders
  • 2
3 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 18764083
Hi fordraiders;

OK lets say you have the following:

31/4   3 1/4
31/8   3 1/8
11/16   1 1/16
21/64   2 1/64
21/32   2 1/32

After being processed what should they look like?

Fernando
0
 
LVL 3

Author Comment

by:fordraiders
ID: 18764199
31/4   3~1/4
31/8   3~1/8
11/16   1~1/16
21/64   2~1/64
21/32   2~1/32

Need a Tilde ~

0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 18764681
Hi fordraiders;

This should do what you need without the need of the StrReplace function.

        dbRow.Item("fldDescription") = Regex.Replace(dbRow.Item("fldDescription"), _
                "\s(\d)(\d/\d+)\s", "$1~$2")

The above pattern will take values like

Values to be replaced            Will be replaced with
          31/4                                         3~1/4
          31/8                                         3~1/8
          11/16                                       1~1/16
          21/64                                       2~1/64
          21/32                                       2~1/32

Fernando
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

757 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now