简体   繁体   中英

Regex pattern to separate string with semicolon and plus

Here I have used the below mentioned code.

MatchCollection matches = Regex.Matches(cellData, @"(^\[.*\]$|^\[.*\]_[0-9]*$)");

The only this pattern is not doing is it's not separating the semicolon and plus from the main string.

A sample string is

[dbServer];[ciDBNAME];[dbLogin];[dbPasswd] AND [SIM_ErrorFound@1]_+[@IterationCount] 

I am trying to extract

[dbServer]
[ciDBNAME]
[dbLogin]
[dbPasswd]
[SIM_ErrorFound@1]
[@IterationCount]

from the string.

To extract the stuff in square brackets from [dbServer];[ciDBNAME];[dbLogin];[dbPasswd] AND [SIM_ErrorFound@1]_+[@IterationCount] (which is what I assume you're be trying to do),

The regular expression (I haven't quoted it) should be

\\[([^\\]]*)\\]

You should not use ^ and $ as youre not interested in start and end of strings. The parentheses will capture every instance of zero or more characters inside square brackets. If you want to be more specific about what you're capturing in the brackets, you'll need to change the [^\\] to something else.

Your regex - (^\\[.*\\]$|^\\[.*\\]_[0-9]*$) - matches any full string that starts with [ , then contains zero or more chars other than a newline, and ends with ] ( \\]$ ) or with _ followed with 0+ digits ( _[0-9]*$ ). You could also write the pattern as ^\\[.*](?:_[0-9]*)?$ and it would work the same.

However, you need to match multiple substrings inside a larger string. Thus, you should have removed the ^ and $ anchors and retried. Then, you would find out that .* is too greedy and matches from the first [ up to the last ] . To fix that, it is best to use a negated character class solution. Eg you may use [^][]* that matches 0+ chars other than [ and ] .

Edit: It seems you need to get only the text inside square brackets.

You need to use a capturing group , a pair of unescaped parentheses around the part of the pattern you need to get and then access the value by the group ID (unnamed groups are numbered starting with 1 from left to right):

var results = Regex.Matches(s, @"\[([^][]+)]")
    .Cast<Match>()
    .Select(m => m.Groups[1].Value)
    .ToList();

See the .NET regex demo 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM