| 1 | #Region "Microsoft.VisualBasic::872b05a19ade60385eb119b14954af63, Microsoft.VisualBasic.Core\Text\Patterns\Regexp.vb" |
| 2 | |
| 3 | ' Author: |
| 4 | ' |
| 5 | ' asuka (amethyst.asuka@gcmodeller.org) |
| 6 | ' xie (genetics@smrucc.org) |
| 7 | ' xieguigang (xie.guigang@live.com) |
| 8 | ' |
| 9 | ' Copyright (c) 2018 GPL3 Licensed |
| 10 | ' |
| 11 | ' |
| 12 | ' GNU GENERAL PUBLIC LICENSE (GPL3) |
| 13 | ' |
| 14 | ' |
| 15 | ' This program is free software: you can redistribute it and/or modify |
| 16 | ' it under the terms of the GNU General Public License as published by |
| 17 | ' the Free Software Foundation, either version 3 of the License, or |
| 18 | ' (at your option) any later version. |
| 19 | ' |
| 20 | ' This program is distributed in the hope that it will be useful, |
| 21 | ' but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 | ' GNU General Public License for more details. |
| 24 | ' |
| 25 | ' You should have received a copy of the GNU General Public License |
| 26 | ' along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 27 | |
| 28 | |
| 29 | |
| 30 | ' /********************************************************************************/ |
| 31 | |
| 32 | ' Summaries: |
| 33 | |
| 34 | ' Class Regexp |
| 35 | ' |
| 36 | ' Properties: Regex |
| 37 | ' |
| 38 | ' Constructor: (+1 Overloads) Sub New |
| 39 | ' Function: ToString |
| 40 | ' Operators: <=, >=, (+4 Overloads) And, (+4 Overloads) Like |
| 41 | ' |
| 42 | ' |
| 43 | ' /********************************************************************************/ |
| 44 | |
| 45 | #End Region |
| 46 | |
| 47 | Imports System.Runtime.CompilerServices |
| 48 | Imports System.Text.RegularExpressions |
| 49 | |
| 50 | Namespace Text.Patterns |
| 51 | |
| 52 | Public Class Regexp |
| 53 | |
| 54 | ReadOnly r As Regex |
| 55 | ReadOnly pattern$ |
| 56 | |
| 57 | Public ReadOnly Property Regex As String |
| 58 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
| 59 | Get |
| 60 | Return pattern |
| 61 | End Get |
| 62 | End Property |
| 63 | |
| 64 | Sub New(pattern$, Optional opts As RegexOptions = RegexICSng) |
| 65 | Me.r = New Regex(pattern, opts) |
| 66 | Me.pattern = pattern |
| 67 | End Sub |
| 68 | |
| 69 | Public Overrides Function ToString() As String |
| 70 | Return pattern & " @ " & r.Options.ToString |
| 71 | End Function |
| 72 | |
| 73 | ''' <summary> |
| 74 | ''' Grep target input <paramref name="text"/> by a specific regex <paramref name="pattern"/>. |
| 75 | ''' Match all of the sub string in target <paramref name="text"/> that matched the input regex <paramref name="pattern"/>. |
| 76 | ''' (即从目标字符串之中匹配出所有符合目标模式的字符串) |
| 77 | ''' </summary> |
| 78 | ''' <param name="pattern"></param> |
| 79 | ''' <param name="text$"></param> |
| 80 | ''' <returns></returns> |
| 81 | ''' |
| 82 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
| 83 | Public Shared Operator <=(pattern As Regexp, text$) As String() |
| 84 | Return pattern.r.Matches(text).ToArray |
| 85 | End Operator |
| 86 | |
| 87 | Public Shared Operator >=(pattern As Regexp, text$) As String() |
| 88 | Throw New NotSupportedException |
| 89 | End Operator |
| 90 | |
| 91 | ''' <summary> |
| 92 | ''' 求出目标字符串<paramref name="text"/>和<paramref name="pattern"/>的交集,即使用正则表达式来匹配目标字符串之中的子串 |
| 93 | ''' </summary> |
| 94 | ''' <param name="text$"></param> |
| 95 | ''' <param name="pattern"></param> |
| 96 | ''' <returns></returns> |
| 97 | ''' |
| 98 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
| 99 | Public Shared Operator And(text$, pattern As Regexp) As String |
| 100 | Return pattern.r.Match(text).Value |
| 101 | End Operator |
| 102 | |
| 103 | ''' <summary> |
| 104 | ''' 求出目标字符串<paramref name="text"/>和<paramref name="pattern"/>的交集,即使用正则表达式来匹配目标字符串之中的子串 |
| 105 | ''' </summary> |
| 106 | ''' <param name="text$"></param> |
| 107 | ''' <param name="pattern"></param> |
| 108 | ''' <returns></returns> |
| 109 | ''' |
| 110 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
| 111 | Public Shared Operator And(pattern As Regexp, text$) As String |
| 112 | Return pattern.r.Match(text).Value |
| 113 | End Operator |
| 114 | |
| 115 | ''' <summary> |
| 116 | ''' Target input <paramref name="text"/> is equals to the regex <paramref name="pattern"/>? |
| 117 | ''' </summary> |
| 118 | ''' <param name="text$"></param> |
| 119 | ''' <param name="pattern"></param> |
| 120 | ''' <returns></returns> |
| 121 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
| 122 | Public Shared Operator Like(text$, pattern As Regexp) As Boolean |
| 123 | Return (text And pattern) = text |
| 124 | End Operator |
| 125 | |
| 126 | ''' <summary> |
| 127 | ''' Target input <paramref name="text"/> is equals to the regex <paramref name="pattern"/>? |
| 128 | ''' </summary> |
| 129 | ''' <param name="text$"></param> |
| 130 | ''' <param name="pattern"></param> |
| 131 | ''' <returns></returns> |
| 132 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
| 133 | Public Shared Operator Like(pattern As Regexp, text$) As Boolean |
| 134 | Return text Like pattern |
| 135 | End Operator |
| 136 | End Class |
| 137 | End Namespace |