Results 1 to 19 of 19

Thread: regular expression and txt file

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,586

    regular expression and txt file

    I loop a txt to read line by line, with the tipical:

    Open Path For Input As File
    do While Not EOF(File)
    ...
    loop

    ecc....

    i need to find (with REGUALR EXPRESSION) the line in txt where is present the string "CBXXXXXX", if the string is found then Msgbox( the line is in line nr... postion ....)

    for example:
    CB123456
    CB768543
    CB654387

    NOTE:
    - XXXXXX are only a number
    - The lenght of number suffix is always 6 number

    an example please?
    Last edited by luca90; Sep 19th, 2014 at 08:13 AM.

  2. #2
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: regular expression and txt file

    Code:
    Dim strLine As String
    Dim selfile As String
    Dim lineNum As Integer
    selfile = App.Path & "\mytFile.txt"
    Open selfile For Input As #1
    Do While Not EOF(1)
        Line Input #1, strLine
        lineNum = lineNum + 1
            If strLine = "CB768543" Then  'obviously you would use a variable or textbox instead of hardcoding...just an ex
              MsgBox strLine & " is located at line " & CStr(lineNum)
              'Exit Sub  'IF you know the data only occurs once
            End If
    Loop
    Close #1

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,586

    Re: regular expression and txt file

    Quote Originally Posted by SamOscarBrown View Post
    Code:
    Dim strLine As String
    Dim selfile As String
    Dim lineNum As Integer
    selfile = App.Path & "\mytFile.txt"
    Open selfile For Input As #1
    Do While Not EOF(1)
        Line Input #1, strLine
        lineNum = lineNum + 1
            If strLine = "CB768543" Then
              MsgBox strLine & " is located at line " & CStr(lineNum)
              'Exit Sub  'IF you know the data only occurs once
            End If
    Loop
    Close #1
    ok tks for code. but the number after CB not is fixed i can after CB, 123456, or 789456, or 548851 ...

    see in my note.

  4. #4
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: regular expression and txt file

    I know.....what NUMBER are you looking for?
    If you use a textbox in which you type the number you are looking for, you can use something like this (see in MY note)

    (type in the textbox the NUMBER (only) you are looking for)
    Code:
    if mid(strLine,3,6) = "text1.txt" then
    ....
    end if

  5. #5
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: regular expression and txt file

    Of course you have to change this to pull the lines from your file instead of a textbox but it should get you started.
    Code:
    Private Sub Command1_Click()
    Dim RegEx As RegExp
    
        Set RegEx = New RegExp
        
        With RegEx
            .Global = True
            .IgnoreCase = True
            .Pattern = "[CB]\d{6}$"
        End With
        
        If RegEx.Test(Text1.Text) Then
            Debug.Print "match"
        Else
            Debug.Print "no match"
        End If
        
        Set RegEx = Nothing
    End Sub

  6. #6
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: regular expression and txt file

    Actually the better pattern would be

    .Pattern = "(^CB\d{6})$"

  7. #7
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,255

    Re: regular expression and txt file

    Being aware that regular expressions were asked for - the VB Like Operator Pattern-Syntax
    is a bit more intuitive IMO (and no slouch either).

    Your test-condition-line (using Like) could be written this way
    (if there's possible characters to the left AND always chars to the right of the CBxxxxxx snippet which is to find)
    If sCurrentLine Like "*CB######[!0-9]*" Then

    BTW Mark, the above case (possible characters preceding the CBxxxxxx snippet - and
    non-numeric characters following it) is currently not covered by the RegExp-Pattern-String.

    In case the CBxxxxxx could occur also at the end of a line (no chars following), then the above
    condition could be enhanced this way:
    If sCurrentLine & " " Like "*CB######[!0-9]*" Then

    In case the CBxxxxxx always occurs at the end of a Line, then one could use:
    If sCurrentLine Like "*CB######" Then

    Olaf
    Last edited by Schmidt; Sep 19th, 2014 at 02:33 PM.

  8. #8
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: regular expression and txt file

    Quote Originally Posted by Schmidt View Post
    BTW Mark, the above case (possible characters preceding the CBxxxxxx snippet - and
    non-numeric characters following it) is currently not covered by the RegExp-Pattern-String.
    You are right. OP asked to match CB with 6 digits proceeding it. If you want to match anywhere within the line the remove the ^ and $.

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,586

    Re: regular expression and txt file

    Quote Originally Posted by MarkT View Post
    You are right. OP asked to match CB with 6 digits proceeding it. If you want to match anywhere within the line the remove the ^ and $.
    hI Mark...Olaf,
    if i have understand to the end of discussion, the correct pattern is:

    "(^CB\d{6})$"
    or
    "[CB]\d{6}$"

    or i need to use simply the code :

    If sCurrentLine & " " Like "*CB######[!0-9]*" Then

    in my loop?

  10. #10
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: regular expression and txt file

    They gave you 2 options: 1 uses regular expressions and 1 uses a VB function. Your choice...
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  11. #11

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,586

    Re: regular expression and txt file

    Quote Originally Posted by LaVolpe View Post
    They gave you 2 options: 1 uses regular expressions and 1 uses a VB function. Your choice...
    ok... in case i use the regex, wath is the correct pattern?
    tks as usual.

    in other case with this code, work only the like statement(?).

    Option Explicit
    Sub LEGGI()

    Dim File As Integer
    Dim TextOfLine As String
    Dim Path As String
    Dim I As Long

    Dim RegEx As RegExp
    Set RegEx = New RegExp

    Path = "C:\TAB\TAB.TXT"

    File = FreeFile(0)
    Open Path For Input As File
    While Not EOF(File)
    Line Input #File, TextOfLine

    With RegEx
    .Global = True
    .IgnoreCase = True
    .Pattern = "[CB]\d{6}$"
    End With

    If RegEx.Test(TextOfLine) Then
    Stop
    Debug.Print "match"
    End If

    If TextOfLine & " " Like "*CB######[!0-9]*" Then
    Stop
    End If

    I = I + 1
    Wend

    Close File

    End Sub
    Last edited by luca90; Sep 19th, 2014 at 04:10 PM.

  12. #12
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: regular expression and txt file

    Here is a little neater version of what you posted showing the RegEx solution and the Like solution.
    Code:
    Sub LEGGI()
    Dim File As Integer
    Dim TextOfLine As String
    Dim Path As String
    Dim I As Long
    Dim RegEx As RegExp
    
        Set RegEx = New RegExp
    
        Path = "C:\TAB\TAB.TXT"
    
        File = FreeFile(0)
        Open Path For Input As File
            While Not EOF(File)
                Line Input #File, TextOfLine
                
                'START OF REGEX CODE
                With RegEx
                    .Global = True
                    .IgnoreCase = True
                    .Pattern = "^(CB\d{6})$"
                End With
    
                If RegEx.Test(TextOfLine) Then
                    Stop
                    Debug.Print "RegEx match"
                End If
                'END OF REGEX CODE
                
                'START OF LIKE CODE
                If TextOfLine & " " Like "*CB######[!0-9]*" Then
                    Stop
                    Debug.Print "Like match"
                End If
                'END OF LIKE CODE
                
                I = I + 1
            Wend
        Close File
    End Sub
    If you decide to go with regular expression then I would move the with block outside of the loop since you don't need to recreate it with every interation of the loop.

  13. #13

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,586

    Re: regular expression and txt file

    Quote Originally Posted by MarkT View Post
    Here is a little neater version of what you posted showing the RegEx solution and the Like solution.
    Code:
    Sub LEGGI()
    Dim File As Integer
    Dim TextOfLine As String
    Dim Path As String
    Dim I As Long
    Dim RegEx As RegExp
    
        Set RegEx = New RegExp
    
        Path = "C:\TAB\TAB.TXT"
    
        File = FreeFile(0)
        Open Path For Input As File
            While Not EOF(File)
                Line Input #File, TextOfLine
                
                'START OF REGEX CODE
                With RegEx
                    .Global = True
                    .IgnoreCase = True
                    .Pattern = "^(CB\d{6})$"
                End With
    
                If RegEx.Test(TextOfLine) Then
                    Stop
                    Debug.Print "RegEx match"
                End If
                'END OF REGEX CODE
                
                'START OF LIKE CODE
                If TextOfLine & " " Like "*CB######[!0-9]*" Then
                    Stop
                    Debug.Print "Like match"
                End If
                'END OF LIKE CODE
                
                I = I + 1
            Wend
        Close File
    End Sub
    If you decide to go with regular expression then I would move the with block outside of the loop since you don't need to recreate it with every interation of the loop.

    HI Mark, the part of regex of your code, dont match the CB with numerical suffix....!!!!

    i can send in pvt a part of m txt file?
    Reserved data:-)

  14. #14
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: regular expression and txt file

    You can email me a sample file and I'll take a look.

  15. #15
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,255

    Re: regular expression and txt file

    If one is not absolutely sure, if a certain "Pattern" is already the correct one -
    it is usually a good idea to check things beforehand with "proper test-cases which leave nothing out".

    Throwing a new thing into "the final routine itself" (and then checking it there with only one File,
    is often not sufficient, because the file in question might not contain a certain "worst case").

    It's often better to come up with these "worst cases" on your own - and simulate them
    in an isolated fashion before integration into the final target-routine takes place.

    One method to do that is TDD: http://en.wikipedia.org/wiki/Test-driven_development

    To develop really TDD-driven, you will have to write your next Test-Line first - then
    make your implementation fail on this new "harder" Test-Case - and adapt the
    implementation (in this case your Pattern-String) accordingly - then introduce the
    next harder TestCase - and repeat the "red-green" cycle again - until all the increasingly
    harder Test-Cases become green - and you left nothing out.

    To make things easier with TDD, there exist certain "TDD-supporting helpers" -
    for VB6 there is one included in vbRichClient5, which allows to formulate Test-Cases
    in one single line.

    To be able to use the cTDD-Class, one has to host the new Routine in question within a Class -
    e.g. this way for the VB-Like-Operator approach in a Class named cLikeCmp:

    Code:
    Option Explicit
    
    Public Function IsMatching(sLine As String, Optional ByVal IgnoreCase As Boolean) As Boolean
      IsMatching = IIf(IgnoreCase, UCase$(sLine), sLine) & " " Like "*CB######[!0-9]*"
    End Function
    That's it already.

    A similar thing one can do for the RegExp-based approach in another Class, e.g. named cRegExp:

    Code:
    Option Explicit
    
    Private RegExp As New RegExp
    
    Private Sub Class_Initialize()
      RegExp.Global = True
    End Sub
    
    Public Function IsMatching(sLine As String, Optional ByVal IgnoreCase As Boolean) As Boolean
      RegExp.IgnoreCase = IgnoreCase
      RegExp.Pattern = "^(CB\d{6})$"
      IsMatching = RegExp.Test(sLine)
    End Function
    With the both Classes in place, what we need (aside from a reference to vbRichClient5) is
    only a *.bas Module which hosts Sub Main (no Form is needed in our separate Test-Project).

    Our Tests can now be written up in Sub Main, line by line - getting ever harder, repeatedly
    pressing F5, to check if every test we wrote so far, is still resulting with "all green" -
    until we are satisfied and have a "well-thought out coverage" of all the possible problems.

    E.g. one could start out with 3 Test-Case lines like the ones below in Sub Main() -
    followed by pressing F5...

    Code:
    Option Explicit
    
    Sub Main()
      
      'let's test the Like-Comparison-Method first (defined in cLikeCmp)
      With New_c.TDD(New cLikeCmp, "IsMatching") '<- give Class and MethodName here
      
        .Test "5 Digits Standalone", False, IsExpected, "CB12345"
        .Test "6 Digits Standalone", True, IsExpected, "CB123456"
        .Test "7 Digits Standalone", False, IsExpected, "CB1234567"
    
        Debug.Print .TestResults
      End With
    
    End Sub
    The Debug.Print line then resulting in the following output in the DirectWindow
    (since our IsMatching-Routine in cLikeCmp is already validated):
    Code:
    TestRun on: cLikeCmp, Method: IsMatching, TestCount: 3
    Success on all Tests (3 total)
    In my "building up" of the complete Testcoverage I used Copy&Paste of the three test-lines
    above (to work further from the 3 different "digit-amounts" of 5, 6 or 7 digits)...
    So, the "amount of work" is not as much as one might think, when looking at the
    complete TestCode below - it's basically done per copy&paste + adapt parameters to make
    the next testcases "harder".

    So, here is the complete "full-coverage" TestCode for Sub Main (for both classes, cLikeCmp and cRegExp):

    Code:
    Option Explicit
    
    Sub Main()
      
      'let's test the Like-Comparison-Method first (defined in cLikeCmp)
      With New_c.TDD(New cLikeCmp, "IsMatching") '<- give Class and MethodName here
      
        .Test "5 Digits Standalone", False, IsExpected, "CB12345"
        .Test "6 Digits Standalone", True, IsExpected, "CB123456"
        .Test "7 Digits Standalone", False, IsExpected, "CB1234567"
        
        .Test "5 Digits preceded by Num", False, IsExpected, "0CB12345"
        .Test "6 Digits preceded by Num", True, IsExpected, "0CB123456"
        .Test "7 Digits preceded by Num", False, IsExpected, "0CB1234567"
    
        .Test "5 Digits preceded by Alpha", False, IsExpected, "xCB12345"
        .Test "6 Digits preceded by Alpha", True, IsExpected, "xCB123456"
        .Test "7 Digits preceded by Alpha", False, IsExpected, "xCB1234567"
    
        .Test "5 Digits preceded by Num and Alpha", False, IsExpected, "0xCB12345"
        .Test "6 Digits preceded by Num and Alpha", True, IsExpected, "0xCB123456"
        .Test "7 Digits preceded by Num and Alpha", False, IsExpected, "0xCB1234567"
    
        .Test "5 Digits pr. by Num and Alpha, trailed by Alpha", False, IsExpected, "0xCB12345x"
        .Test "6 Digits pr. by Num and Alpha, trailed by Alpha", True, IsExpected, "0xCB123456x"
        .Test "7 Digits pr. by Num and Alpha, trailed by Alpha", False, IsExpected, "0xCB1234567x"
    
        .Test "5 Digits pr. by Num and Alpha, trailed by Num", True, IsExpected, "0xCB123450"
        .Test "6 Digits pr. by Num and Alpha, trailed by Num", False, IsExpected, "0xCB1234560"
        .Test "7 Digits pr. by Num and Alpha, trailed by Num", False, IsExpected, "0xCB12345670"
        
        Debug.Print "Tests total:"; .TotalTests
        Debug.Print "Tests failed:"; .FailedTests
        Debug.Print "Pretty printing of the results:"
        Debug.Print .TestResults
      End With
      
      
      'test the RegExp-Comparison-Method now (defined in cRegExp)
      With New_c.TDD(New cRegExp, "IsMatching") '<- give Class and MethodName here
      
        .Test "5 Digits Standalone", False, IsExpected, "CB12345"
        .Test "6 Digits Standalone", True, IsExpected, "CB123456"
        .Test "7 Digits Standalone", False, IsExpected, "CB1234567"
        
        .Test "5 Digits preceded by Num", False, IsExpected, "0CB12345"
        .Test "6 Digits preceded by Num", True, IsExpected, "0CB123456"
        .Test "7 Digits preceded by Num", False, IsExpected, "0CB1234567"
        
        .Test "5 Digits preceded by Alpha", False, IsExpected, "xCB12345"
        .Test "6 Digits preceded by Alpha", True, IsExpected, "xCB123456"
        .Test "7 Digits preceded by Alpha", False, IsExpected, "xCB1234567"
        
        .Test "5 Digits preceded by Num and Alpha", False, IsExpected, "0xCB12345"
        .Test "6 Digits preceded by Num and Alpha", True, IsExpected, "0xCB123456"
        .Test "7 Digits preceded by Num and Alpha", False, IsExpected, "0xCB1234567"
        
        .Test "5 Digits pr. by Num and Alpha, trailed by Alpha", False, IsExpected, "0xCB12345x"
        .Test "6 Digits pr. by Num and Alpha, trailed by Alpha", True, IsExpected, "0xCB123456x"
        .Test "7 Digits pr. by Num and Alpha, trailed by Alpha", False, IsExpected, "0xCB1234567x"
        
        .Test "5 Digits pr. by Num and Alpha, trailed by Num", True, IsExpected, "0xCB123450"
        .Test "6 Digits pr. by Num and Alpha, trailed by Num", False, IsExpected, "0xCB1234560"
        .Test "7 Digits pr. by Num and Alpha, trailed by Num", False, IsExpected, "0xCB12345670"
        
        Debug.Print "Tests total:"; .TotalTests
        Debug.Print "Tests failed:"; .FailedTests
        Debug.Print "Pretty printing of the results:"
        Debug.Print .TestResults
      End With
     
    End Sub
    And here's the Debug-Output, which in case of cLikeCmp is already "completely green" -
    whilst in case of cRegExp there's still a little tweaking of the Pattern-String necessary
    (I leave that to you Mark - the Test-coverage is really nice to use for this last validation-
    step, in case you're interested in trying out the cTDD-Class of the RC5).

    Code:
    Tests total: 18 
    Tests failed: 0 
    Pretty printing of the results:
    TestRun on: cLikeCmp, Method: IsMatching, TestCount: 18
    Success on all Tests (18 total)
    
    Tests total: 18 
    Tests failed: 5 
    Pretty printing of the results:
    TestRun on: cRegExp, Method: IsMatching, TestCount: 18
    5 Test(s) failed
      Test 5 (6 Digits preceded by Num) Res: True was expected, but got: False
      Test 8 (6 Digits preceded by Alpha) Res: True was expected, but got: False
      Test 11 (6 Digits preceded by Num and Alpha) Res: True was expected, but got: False
      Test 14 (6 Digits pr. by Num and Alpha, trailed by Alpha) Res: True was expected, but got: False
      Test 16 (5 Digits pr. by Num and Alpha, trailed by Num) Res: True was expected, but got: False
    Olaf
    Last edited by Schmidt; Sep 20th, 2014 at 05:16 PM.

  16. #16
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: regular expression and txt file

    Quote Originally Posted by Schmidt View Post
    And here's the Debug-Output, which in case of cLikeCmp is already "completely green" -
    whilst in case of cRegExp there's still a little tweaking of the Pattern-String necessary
    (I leave that to you Mark - the Test-coverage is really nice to use for this last validation-
    step, in case you're interested in trying out the cTDD-Class of the RC5).
    I really haven't looked at your code to see how it is testing things but the RegEx pattern that I'm using "^(CB\d{6})$" should only match CB followed by 6 digits. It will not match CB followed by 6 digits if it is surrounded by other text. Basically the ^ and $ are saying that everything from the start to the end of the line must match the pattern.

    Use ^CB\d{6} if you want to make sure the line start with the pattern but you don't care if there is text after the 6 digits.
    Use CB\d{6}$ if you don't care if there are characters before CB but the 6 digits are the end of the line.
    And if all you want to know is if there is a match to the pattern anywhere in the line then use CB\d{6}

    with the pattern the only line I would expect to match is
    .Test "6 Digits Standalone", True, IsExpected, "CB123456"

    The pattern I came up with was based on OP asking to match a line that starts with CB followed by 6 digits.
    Last edited by MarkT; Sep 20th, 2014 at 11:19 AM.

  17. #17
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,255

    Re: regular expression and txt file

    Quote Originally Posted by MarkT View Post
    ...if all you want to know is if there is a match to the pattern anywhere in the line then use CB\d{6}
    Ok, when I adapt the Code in cRegExp to the pattern above:

    Code:
    Option Explicit
    
    Private RegExp As New RegExp
    
    Private Sub Class_Initialize()
      RegExp.Global = True
    End Sub
    
    Public Function IsMatching(sLine As String, Optional ByVal IgnoreCase As Boolean) As Boolean
      RegExp.IgnoreCase = IgnoreCase
      RegExp.Pattern = "CB\d{6}"
      IsMatching = RegExp.Test(sLine)
    End Function
    This new implementation will produce the following results over the Test-coverage:
    Code:
    TestRun on: cRegExp, Method: IsMatching, TestCount: 18
    7 Test(s) failed
      Test 3 (7 Digits Standalone) Res: False was expected, but got: True
      Test 6 (7 Digits preceded by Num) Res: False was expected, but got: True
      Test 9 (7 Digits preceded by Alpha) Res: False was expected, but got: True
      Test 12 (7 Digits preceded by Num and Alpha) Res: False was expected, but got: True
      Test 15 (7 Digits pr. by Num and Alpha, trailed by Alpha) Res: False was expected, but got: True
      Test 17 (6 Digits pr. by Num and Alpha, trailed by Num) Res: False was expected, but got: True
      Test 18 (7 Digits pr. by Num and Alpha, trailed by Num) Res: False was expected, but got: True
    Not meant as "criticism" of your recommended Pattern-Strings - since "in theory" the pattern "CB\d{6}"
    should produce less (or zero) errors over the test-coverage (although since there's slight differences in
    the implementation of RegEx-Helper-classes, concrete Tests are always worthwhile).

    In practice, using the VBScript.RegExp-Class (and to finally come up with something which fullfills the
    range of tests in the same way as the Like-Expression) - the Pattern would have to be: "CB\d{6}(\b|\D)"

    Quote Originally Posted by MarkT View Post
    The pattern I came up with was based on OP asking to match a line that starts with CB followed by 6 digits.
    I know - the OP was not very clear about the concrete contents of his Text-Lines
    (on what the potentially preceding or trailing chars might be).

    It's up to him, to reduce the amount of Tests in a way, which is sufficient in his concrete scenario.
    Then other (simpler) RegEx-Patterns might already be successful in passing them all.

    Olaf

  18. #18
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: regular expression and txt file

    Based on the results you are posting, I'm not sure you understand what the pattern CB\d{6} should do. The result you got are exactly what I would have expected.

    ^CB\d{6}$ = True for an exact match of the test string
    ^CB\d{6} = True if the test string starts with CBxxxxxx. It would also be true if the string has characters after the CB and 6 digits
    CB\d{6}$ = True if the test string ends with CBxxxxxx. It would also be true if the string has characters before the CB and 6 digits
    CB\d{6} = True if the test string contains CB and 6 digits (think InStr)

    So this is what I would expect with your test examples. Let me know if you are getting different results.

    ^CB\d{6}$ [Test string must match exactly]
    5 Digits Standalone "CB12345" = False
    6 Digits Standalone "CB123456" = True
    7 Digits Standalone "CB1234567" = False

    5 Digits preceded by Num "0CB12345" = False
    6 Digits preceded by Num "0CB123456" = False
    7 Digits preceded by Num "0CB1234567" = False

    5 Digits preceded by Alpha "xCB12345" = False
    6 Digits preceded by Alpha "xCB123456" = False
    7 Digits preceded by Alpha "xCB1234567" = False

    5 Digits preceded by Num and Alpha "0xCB12345" = False
    6 Digits preceded by Num and Alpha "0xCB123456" = False
    7 Digits preceded by Num and Alpha "0xCB1234567" = False

    5 Digits pr. by Num and Alpha, trailed by Alpha "0xCB12345x" = False
    6 Digits pr. by Num and Alpha, trailed by Alpha "0xCB123456x" = False
    7 Digits pr. by Num and Alpha, trailed by Alpha "0xCB1234567x" = False

    5 Digits pr. by Num and Alpha, trailed by Num "0xCB123450" = False
    6 Digits pr. by Num and Alpha, trailed by Num "0xCB1234560" = False
    7 Digits pr. by Num and Alpha, trailed by Num "0xCB12345670" = False


    ^CB\d{6} [Test string begins with CBxxxxxx
    5 Digits Standalone "CB12345" = False
    6 Digits Standalone "CB123456" = True
    7 Digits Standalone "CB1234567" = True

    5 Digits preceded by Num "0CB12345" = False
    6 Digits preceded by Num "0CB123456" = False
    7 Digits preceded by Num "0CB1234567" = False

    5 Digits preceded by Alpha "xCB12345" = False
    6 Digits preceded by Alpha "xCB123456" = False
    7 Digits preceded by Alpha "xCB1234567" = False

    5 Digits preceded by Num and Alpha "0xCB12345" = False
    6 Digits preceded by Num and Alpha "0xCB123456" = False
    7 Digits preceded by Num and Alpha "0xCB1234567" = False

    5 Digits pr. by Num and Alpha, trailed by Alpha "0xCB12345x" = False
    6 Digits pr. by Num and Alpha, trailed by Alpha "0xCB123456x" = False
    7 Digits pr. by Num and Alpha, trailed by Alpha "0xCB1234567x" = False

    5 Digits pr. by Num and Alpha, trailed by Num "0xCB123450" = False
    6 Digits pr. by Num and Alpha, trailed by Num "0xCB1234560" = False
    7 Digits pr. by Num and Alpha, trailed by Num "0xCB12345670" = False


    CB\d{6}$ [Ends with CBxxxxxx]
    5 Digits Standalone "CB12345" = False
    6 Digits Standalone "CB123456" = True
    7 Digits Standalone "CB1234567" = False

    5 Digits preceded by Num "0CB12345" = False
    6 Digits preceded by Num "0CB123456" = True
    7 Digits preceded by Num "0CB1234567" = False

    5 Digits preceded by Alpha "xCB12345" = False
    6 Digits preceded by Alpha "xCB123456" = True
    7 Digits preceded by Alpha "xCB1234567" = False

    5 Digits preceded by Num and Alpha "0xCB12345" = False
    6 Digits preceded by Num and Alpha "0xCB123456" = True
    7 Digits preceded by Num and Alpha "0xCB1234567" = False

    5 Digits pr. by Num and Alpha, trailed by Alpha "0xCB12345x" = False
    6 Digits pr. by Num and Alpha, trailed by Alpha "0xCB123456x" = False
    7 Digits pr. by Num and Alpha, trailed by Alpha "0xCB1234567x" = False

    5 Digits pr. by Num and Alpha, trailed by Num "0xCB123450" = False
    6 Digits pr. by Num and Alpha, trailed by Num "0xCB1234560" = False
    7 Digits pr. by Num and Alpha, trailed by Num "0xCB12345670" = False


    CB\d{6} [Test string contains CBxxxxxx]
    5 Digits Standalone "CB12345" = False
    6 Digits Standalone "CB123456" = True
    7 Digits Standalone "CB1234567" = True

    5 Digits preceded by Num "0CB12345" = False
    6 Digits preceded by Num "0CB123456" = True
    7 Digits preceded by Num "0CB1234567" = True

    5 Digits preceded by Alpha "xCB12345" = False
    6 Digits preceded by Alpha "xCB123456" = True
    7 Digits preceded by Alpha "xCB1234567" = True

    5 Digits preceded by Num and Alpha "0xCB12345" = False
    6 Digits preceded by Num and Alpha "0xCB123456" = True
    7 Digits preceded by Num and Alpha "0xCB1234567" = True

    5 Digits pr. by Num and Alpha, trailed by Alpha "0xCB12345x" = False
    6 Digits pr. by Num and Alpha, trailed by Alpha "0xCB123456x" = True
    7 Digits pr. by Num and Alpha, trailed by Alpha "0xCB1234567x" = True

    5 Digits pr. by Num and Alpha, trailed by Num "0xCB123450" = False
    6 Digits pr. by Num and Alpha, trailed by Num "0xCB1234560" = True
    7 Digits pr. by Num and Alpha, trailed by Num "0xCB12345670" = True

  19. #19
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,255

    Re: regular expression and txt file

    @Mark
    ...was just trying to be nice...

    @luca90
    in case you were overlooking it in my post #17 -
    and (according to your own post #11) - if you want the RegExp to behave similar to the Like Expression,
    you should use the pattern:
    "CB\d{6}(\b|\D)"
    or somewhat shorter:
    "CB\d{6}($|\D)"

    Olaf

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width