# RegEx Pattern Suggestions...

**URL:** <https://forum.xojo.com/t/regex-pattern-suggestions/13472>\
**Category:** General\
**Created:** [January 7, 2014, 3:41pm UTC](https://forum.xojo.com/t/regex-pattern-suggestions/13472 "2014-01-07T15:41:31Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mike\_Cotrone](https://forum.xojo.com/user_avatar/forum.xojo.com/mike_cotrone/32/19940_2.png) [@Mike\_Cotrone](https://forum.xojo.com/u/Mike_Cotrone)\
**Post date:** [January 7, 2014, 3:41pm UTC](https://forum.xojo.com/t/regex-pattern-suggestions/13472/1 "2014-01-07T15:41:31Z")

</div>

Hello RegEx-ers. I am trying to parse through the following data looking match the following. I can get a hack to make it work, but I am sure there can be a much better way 🙂

Data that Im trying to parse:

```auto
2960S-1#sh license 
Index 1 Feature: lanlite        
        Period left: 0 minute 0 second  
Index 2 Feature: lanbase        
        Period left: Life time
        License Type: Permanent
        License State: Active, In Use
        License Priority: Medium
        License Count: Non-Counted
```

Current Reg Ex Match Pattern that semi works. I had to use an OR BUT I want this to be AND not OR however I just couldn’t make it work with AND.  
Very UGLY 🙂

```auto
Options Set True: Greedy, Allow Empty Matches, String Begin/End is line begin/end, Line End type any.
(?<=Index\\s\\d\\sFeature:\\s).+(?=\\s)|(?<=\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\sPeriod\\sLeft:\\s).+(?=\\s)
```

Thank you for your help once again.  
Mike

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [January 7, 2014, 3:51pm UTC](https://forum.xojo.com/t/regex-pattern-suggestions/13472/2 "2014-01-07T15:51:38Z")

</div>

What do you want to extract from the data when you’re done? It looks like you want:

```auto
lanlite
0 minute 0 second
lanbase
Life time
```

Is that it?

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [January 7, 2014, 4:01pm UTC](https://forum.xojo.com/t/regex-pattern-suggestions/13472/3 "2014-01-07T16:01:58Z")

</div>

Oh, I see what you’re trying to do. You want to capture both parts in the same pattern for each section. This will do it:

```auto
"(?mi-Us)Index \\d Feature: (.*)\
\\s*Period left: (.*)"
```

The feature will be in SubExpressionString(1) and the period in SubExpressionString(2). Trim each result to get rid of the trailing spaces.

---

<div class="post-metadata">

**Author:** ![Mike\_Cotrone](https://forum.xojo.com/user_avatar/forum.xojo.com/mike_cotrone/32/19940_2.png) [@Mike\_Cotrone](https://forum.xojo.com/u/Mike_Cotrone)\
**Post date:** [January 7, 2014, 4:02pm UTC](https://forum.xojo.com/t/regex-pattern-suggestions/13472/4 "2014-01-07T16:02:23Z")

</div>

Kem this is a bit closer but still ugly.

(Index\s\d)|(?\<=Feature:\s).+(?=\s)|(?\<=Period\sleft:\s).+(?=\s)|(?\<=License\sType:\s).+(?=\s)|(?\<=License\sState:\s).+(?=\s)|(?\<=License\sPriority:\s).+(?=\s)|(?\<=License\sCount:\s).+(?=\s)

---

<div class="post-metadata">

**Author:** ![Mike\_Cotrone](https://forum.xojo.com/user_avatar/forum.xojo.com/mike_cotrone/32/19940_2.png) [@Mike\_Cotrone](https://forum.xojo.com/u/Mike_Cotrone)\
**Post date:** [January 7, 2014, 4:02pm UTC](https://forum.xojo.com/t/regex-pattern-suggestions/13472/5 "2014-01-07T16:02:58Z")

</div>

[quote=57623:@Kem Tekinay]Oh, I see what you’re trying to do. You want to capture both parts in the same pattern for each section. This will do it:

```auto
"(?mi-Us)Index \\d Feature: (.*)\
\\s*Period left: (.*)"
```

The feature will be in SubExpressionString(1) and the period in SubExpressionString(2). Trim each result to get rid of the trailing spaces.[/quote]  
Ooooh nice thanks Kem! Something new to try 🙂 Awesome and thanks!

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [January 7, 2014, 4:04pm UTC](https://forum.xojo.com/t/regex-pattern-suggestions/13472/6 "2014-01-07T16:04:22Z")

</div>

No problem.

This pattern assumes you are only interested in those two items, and that the Period Left line will always follow the Index Feature line.

---

<div class="post-metadata">

**Author:** ![Mike\_Cotrone](https://forum.xojo.com/user_avatar/forum.xojo.com/mike_cotrone/32/19940_2.png) [@Mike\_Cotrone](https://forum.xojo.com/u/Mike_Cotrone)\
**Post date:** [January 7, 2014, 4:11pm UTC](https://forum.xojo.com/t/regex-pattern-suggestions/13472/7 "2014-01-07T16:11:44Z")

</div>

Thanks! I am trying it now. 🙂 I keep forgetting you can look inside of SubExpressionString like that.

---

<div class="post-metadata">

**Author:** ![Mike\_Cotrone](https://forum.xojo.com/user_avatar/forum.xojo.com/mike_cotrone/32/19940_2.png) [@Mike\_Cotrone](https://forum.xojo.com/u/Mike_Cotrone)\
**Post date:** [January 7, 2014, 4:58pm UTC](https://forum.xojo.com/t/regex-pattern-suggestions/13472/8 "2014-01-07T16:58:32Z")

</div>

Kem I can’t get it to match and I think it’s because I am storing the data in an array so it may be throwing off the pattern a bit. Do you mind spot checking my code? If I cut the match pattern in half (?mi-Us)Index \d Feature: (.\*) it matches.

Thank you again so much.

```auto
  Dim License_RegEx as RegEx
  Dim License_RegExMatch as RegExMatch
  Dim Feature_HitText as String
  Dim PeriodLeft_HitText as String
  License_RegEx = New RegEx
  License_RegEx.Options.CaseSensitive = False
  License_RegEx.Options.Greedy = True
  License_RegEx.Options.MatchEmpty = True
  License_RegEx.Options.DotMatchAll = False
  License_RegEx.Options.StringBeginIsLineBegin = True
  License_RegEx.Options.StringEndIsLineEnd = True
  License_RegEx.Options.TreatTargetAsOneLine = False
  License_RegEx.SearchPattern = "(?mi-Us)Index \\d Feature: (.*)\
\\s*Period left: (.*)"
  
  for i as integer = 0 to UBound(Cisco_ShowLicenseArrayResults)
    License_RegExMatch = License_RegEx.Search(Cisco_ShowLicenseArrayResults(i))
    while License_RegExMatch <> nil
      if License_RegExMatch.SubExpressionString(1) <> "" Then
        Feature_HitText = License_RegExMatch.SubExpressionString(1)
      end if
      if License_RegExMatch.SubExpressionString(2) <> "" Then
        PeriodLeft_HitText = License_RegExMatch.SubExpressionString(2)
      end if
      License_RegExMatch = License_RegEx.Search
    Wend
  Next i

```

---

<div class="post-metadata">

**Author:** ![Mike\_Cotrone](https://forum.xojo.com/user_avatar/forum.xojo.com/mike_cotrone/32/19940_2.png) [@Mike\_Cotrone](https://forum.xojo.com/u/Mike_Cotrone)\
**Post date:** [January 7, 2014, 5:14pm UTC](https://forum.xojo.com/t/regex-pattern-suggestions/13472/9 "2014-01-07T17:14:16Z")

</div>

I tried flattening the Array into a flat string with no luck matching.

```auto
  Dim License_RegEx as RegEx
  Dim License_RegExMatch as RegExMatch
  Dim Feature_HitText as String
  Dim PeriodLeft_HitText as String
  License_RegEx = New RegEx
  License_RegEx.Options.CaseSensitive = False
  License_RegEx.Options.Greedy = True
  License_RegEx.Options.MatchEmpty = True
  License_RegEx.Options.DotMatchAll = False
  License_RegEx.Options.StringBeginIsLineBegin = True
  License_RegEx.Options.StringEndIsLineEnd = True
  License_RegEx.Options.TreatTargetAsOneLine = False
  License_RegEx.SearchPattern = "(?mi-Us)Index \\d Feature: (.*)\
\\s*Period left: (.*)"
  
// Flatten Out Array for Testing
  Dim FormattedString as String 
  for i as integer = 0 to UBound(Cisco_ShowLicenseArrayResults)
    FormattedString = FormattedString + Cisco_ShowLicenseArrayResults(i)
  Next i
  
  License_RegExMatch = License_RegEx.Search(FormattedString)
  while License_RegExMatch <> nil
    if License_RegExMatch.SubExpressionString(1) <> "" Then
      Feature_HitText = License_RegExMatch.SubExpressionString(1)
    end if
    if License_RegExMatch.SubExpressionString(2) <> "" Then
      PeriodLeft_HitText = License_RegExMatch.SubExpressionString(2)
    end if
    License_RegExMatch = License_RegEx.Search
  Wend
```

---

<div class="post-metadata">

**Author:** ![jim\_mckay](https://forum.xojo.com/user_avatar/forum.xojo.com/jim_mckay/32/905_2.png) [@jim\_mckay](https://forum.xojo.com/u/jim_mckay)\
**Post date:** [January 7, 2014, 5:30pm UTC](https://forum.xojo.com/t/regex-pattern-suggestions/13472/10 "2014-01-07T17:30:30Z")

</div>

try  
FormattedString = Cisco\_ShowLicenseArrayResults.join(EndOfLine.UNIX)

It’s working here when I do the search on the text as a string. Looks like your flattening code doesn’t add linebreaks.

---

<div class="post-metadata">

**Author:** ![Mike\_Cotrone](https://forum.xojo.com/user_avatar/forum.xojo.com/mike_cotrone/32/19940_2.png) [@Mike\_Cotrone](https://forum.xojo.com/u/Mike_Cotrone)\
**Post date:** [January 7, 2014, 5:31pm UTC](https://forum.xojo.com/t/regex-pattern-suggestions/13472/11 "2014-01-07T17:31:06Z")

</div>

Thanks Jim! Ill try that.

---

<div class="post-metadata">

**Author:** ![Mike\_Cotrone](https://forum.xojo.com/user_avatar/forum.xojo.com/mike_cotrone/32/19940_2.png) [@Mike\_Cotrone](https://forum.xojo.com/u/Mike_Cotrone)\
**Post date:** [January 7, 2014, 5:42pm UTC](https://forum.xojo.com/t/regex-pattern-suggestions/13472/12 "2014-01-07T17:42:48Z")

</div>

Something is weird here. I can copy the results I get from the Formatted string into RegExRx and it matches fine, but it never matches in Xojo. Am I missing something? I am using 2013R4.1

```auto
  Dim License_RegEx as RegEx
  Dim License_RegExMatch as RegExMatch
  Dim Feature_HitText as String
  Dim PeriodLeft_HitText as String
  License_RegEx = New RegEx
  License_RegEx.Options.CaseSensitive = False
  License_RegEx.Options.Greedy = True
  License_RegEx.Options.MatchEmpty = True
  License_RegEx.Options.DotMatchAll = False
  License_RegEx.Options.StringBeginIsLineBegin = True
  License_RegEx.Options.StringEndIsLineEnd = True
  License_RegEx.Options.TreatTargetAsOneLine = False
  License_RegEx.SearchPattern = "(?mi-Us)Index \\d Feature: (.*)\
\\s*Period left: (.*)"
  
  // Flatten Out Array for Testing
  Dim FormattedString as String = Join(Cisco_ShowLicenseArrayResults,EndOfLine.UNIX)
  
  License_RegExMatch = License_RegEx.Search(FormattedString)
  while License_RegExMatch <> nil
    if License_RegExMatch.SubExpressionString(1) <> "" Then
      Feature_HitText = License_RegExMatch.SubExpressionString(1)
    end if
    if License_RegExMatch.SubExpressionString(2) <> "" Then
      PeriodLeft_HitText = License_RegExMatch.SubExpressionString(2)
    end if
    License_RegExMatch = License_RegEx.Search
  Wend
```

---

<div class="post-metadata">

**Author:** ![jim\_mckay](https://forum.xojo.com/user_avatar/forum.xojo.com/jim_mckay/32/905_2.png) [@jim\_mckay](https://forum.xojo.com/u/jim_mckay)\
**Post date:** [January 7, 2014, 5:54pm UTC](https://forum.xojo.com/t/regex-pattern-suggestions/13472/13 "2014-01-07T17:54:39Z")

</div>

It works here with 2013r3.3/mac

---

<div class="post-metadata">

**Author:** ![Mike\_Cotrone](https://forum.xojo.com/user_avatar/forum.xojo.com/mike_cotrone/32/19940_2.png) [@Mike\_Cotrone](https://forum.xojo.com/u/Mike_Cotrone)\
**Post date:** [January 7, 2014, 5:55pm UTC](https://forum.xojo.com/t/regex-pattern-suggestions/13472/14 "2014-01-07T17:55:54Z")

</div>

Thanks Jim … Im Mac 2013r4.1… Ill try 3.3. Thanks!

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [January 7, 2014, 6:04pm UTC](https://forum.xojo.com/t/regex-pattern-suggestions/13472/15 "2014-01-07T18:04:47Z")

</div>

This is my test code in 2014r1, and it works perfectly. I created the array by copying the text you gave us above and converting it to an array.

```auto

  Dim License_RegEx as RegEx
  Dim License_RegExMatch as RegExMatch
  Dim Feature_HitText as String
  Dim PeriodLeft_HitText as String
  License_RegEx = New RegEx
  License_RegEx.Options.CaseSensitive = False
  License_RegEx.Options.Greedy = True
  License_RegEx.Options.MatchEmpty = True
  License_RegEx.Options.DotMatchAll = False
  License_RegEx.Options.StringBeginIsLineBegin = True
  License_RegEx.Options.StringEndIsLineEnd = True
  License_RegEx.Options.TreatTargetAsOneLine = False
  License_RegEx.SearchPattern = "(?mi-Us)Index \\d Feature: (.*)\
\\s*Period left: (.*)"
  
  dim Cisco_ShowLicenseArrayResults() as string = Array( _
  "2960S-1#sh license ", _
  "Index 1 Feature: lanlite ", _
  " Period left: 0 minute 0 second ", _
  "Index 2 Feature: lanbase ", _
  " Period left: Life time", _
  " License Type: Permanent", _
  " License State: Active, In Use", _
  " License Priority: Medium", _
  " License Count: Non-Counted" _
  )
  
  // Flatten Out Array for Testing
  Dim FormattedString as String = Join(Cisco_ShowLicenseArrayResults,EndOfLine.UNIX)
  
  License_RegExMatch = License_RegEx.Search(FormattedString)
  while License_RegExMatch <> nil
    if License_RegExMatch.SubExpressionString(1) <> "" Then
      Feature_HitText = License_RegExMatch.SubExpressionString(1)
    end if
    if License_RegExMatch.SubExpressionString(2) <> "" Then
      PeriodLeft_HitText = License_RegExMatch.SubExpressionString(2)
    end if
    License_RegExMatch = License_RegEx.Search
  Wend

```

---

<div class="post-metadata">

**Author:** ![Mike\_Cotrone](https://forum.xojo.com/user_avatar/forum.xojo.com/mike_cotrone/32/19940_2.png) [@Mike\_Cotrone](https://forum.xojo.com/u/Mike_Cotrone)\
**Post date:** [January 7, 2014, 6:09pm UTC](https://forum.xojo.com/t/regex-pattern-suggestions/13472/16 "2014-01-07T18:09:36Z")

</div>

Thank you so much Kem and Jim… I do appreciate the extra effort helping me on this! It appears that my Cisco device (as Jim mentioned earlier) is building my array dynamically differently (endofline) then the test code. The test code works perfectly so Ill get on streamlining the dynamically built array.

Once again you guys have gone above and beyond on this Forum!

---

<div class="post-metadata">

**Author:** ![Mike\_Cotrone](https://forum.xojo.com/user_avatar/forum.xojo.com/mike_cotrone/32/19940_2.png) [@Mike\_Cotrone](https://forum.xojo.com/u/Mike_Cotrone)\
**Post date:** [January 7, 2014, 6:15pm UTC](https://forum.xojo.com/t/regex-pattern-suggestions/13472/17 "2014-01-07T18:15:24Z")

</div>

Ok here is the discrepancies:

My Array that is built dynamically:  
 ![](http://i39.tinypic.com/2qapkqd.png)

Yours (Works):  
 ![](http://i43.tinypic.com/2dmb1np.png)

Notice the indentation differences 🙂 Thanks again guys!

---

<div class="post-metadata">

**Author:** ![Mike\_Cotrone](https://forum.xojo.com/user_avatar/forum.xojo.com/mike_cotrone/32/19940_2.png) [@Mike\_Cotrone](https://forum.xojo.com/u/Mike_Cotrone)\
**Post date:** [January 7, 2014, 6:22pm UTC](https://forum.xojo.com/t/regex-pattern-suggestions/13472/18 "2014-01-07T18:22:10Z")

</div>

Finally I rewrote the line endings and it now works with my dynamically built array. THANKS again!

```auto
  Dim License_RegEx as RegEx
  Dim License_RegExMatch as RegExMatch
  Dim Feature_HitText as String
  Dim PeriodLeft_HitText as String
  License_RegEx = New RegEx
  License_RegEx.Options.CaseSensitive = False
  License_RegEx.Options.Greedy = True
  License_RegEx.Options.MatchEmpty = True
  License_RegEx.Options.DotMatchAll = False
  License_RegEx.Options.StringBeginIsLineBegin = True
  License_RegEx.Options.StringEndIsLineEnd = True
  License_RegEx.Options.TreatTargetAsOneLine = False
  License_RegEx.SearchPattern = "(?mi-Us)Index\\s\\d\\sFeature:\\s(.*)\\s*Period\\sleft:\\s(.*)"
  
  Dim formattedstring, NewString as string 
  
  For i as integer = 0 to UBound(Cisco_ShowLicenseArrayResults)
    formattedstring = formattedstring + Cisco_ShowLicenseArrayResults(i)
  Next i
  
  NewString =ReplaceLineEndings(formattedstring,EndOfLine.UNIX)
  
  License_RegExMatch = License_RegEx.Search(NewString)
  while License_RegExMatch <> nil
    if License_RegExMatch.SubExpressionString(1) <> "" Then
      Feature_HitText = License_RegExMatch.SubExpressionString(1)
    end if
    if License_RegExMatch.SubExpressionString(2) <> "" Then
      PeriodLeft_HitText = License_RegExMatch.SubExpressionString(2)
    end if
    License_RegExMatch = License_RegEx.Search
  Wend
```

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [January 7, 2014, 6:35pm UTC](https://forum.xojo.com/t/regex-pattern-suggestions/13472/19 "2014-01-07T18:35:52Z")

</div>

FYI, `Join` does what your loop does, but does it much faster. The problem is, your array contains the line endings already, so this should work for you instead:

```auto
formattedstring = Join( Cisco_ShowLicenseArrayResults, "" )
NewString = ReplaceLineEndings( formattedstring, EndOfLine.UNIX )
```

---

<div class="post-metadata">

**Author:** ![Mike\_Cotrone](https://forum.xojo.com/user_avatar/forum.xojo.com/mike_cotrone/32/19940_2.png) [@Mike\_Cotrone](https://forum.xojo.com/u/Mike_Cotrone)\
**Post date:** [January 7, 2014, 6:36pm UTC](https://forum.xojo.com/t/regex-pattern-suggestions/13472/20 "2014-01-07T18:36:38Z")

</div>

Awesome thanks Kem!

[Next page](https://forum.xojo.com/t/regex-pattern-suggestions/13472.md?page=2)
