Regex match text outside of brackets

Regex match text outside of brackets



I'm currently using this regex but can figure out how to get the text group to combine its result.


String: Text 1^%+TAB(CMD 1CMD 2)Text 2.^(abc)

Regex: (?<special>[^+%]*?[(][^)]*[)])|(?<special>[^+%]*?[][^]*[}])|(?<text>.)

Result:
text: T
text: e
text: x
text: t
text:
text: 1
special: ^%+TAB
special: (CMD 1CMD 2)
text: T
text: e
text: x
text: t
text:
text: 2
special: ^(abc)

Wanted:
text: Text 1
special: ^%+TAB
special: (CMD 1CMD 2)
text: Text 2
special: ^(abc)



Ultimately I want "Text 1" and "Text 2" to be two groups in the text group. Can't seem to get the text group not to interfere with the special group when adding .*?(..) for the life of me.






Try (?<special>[+^%]*(?:([^)]*)|[^]*}))|(?<text>[ws]+)

– Wiktor Stribiżew
Sep 10 '18 at 9:46



(?<special>[+^%]*(?:([^)]*)|[^]*}))|(?<text>[ws]+)






^(Text 1).*(Text 2).*

– Nhan Phan
Sep 10 '18 at 9:51




2 Answers
2



You may use


(?<special>[+^%]*(?:([^)]*)|[^]*}))|(?<text>[ws]+)



See the regex demo.



Details


(?<special>[+^%]*(?:([^)]*)|[^]*}))


[+^%]*


+


^


%


(?:


([^)]*)


(


)


)


|


[^]*}




}


)


|


(?<text>[ws]+)






How would I match +^% without { or ( in the text group in this string? TEXT 1SPECIAL 1TEXT ^+% 2+^%(SPECIAL +^% 2)

– Vnsax
Sep 16 '18 at 5:11



TEXT 1SPECIAL 1TEXT ^+% 2+^%(SPECIAL +^% 2)






@Vnsax It is impossible to match discontinuous texts into one capturing group. You may replace all of those chars with an empty string, e.g. match.Groups["special"].Value.Replace("[", "").Replace("]", "").Replace("", "").Replace("", "") or Regex.Replace(match.Groups["special"].Value, @"[]+", "")

– Wiktor Stribiżew
Sep 16 '18 at 9:19


match.Groups["special"].Value.Replace("[", "").Replace("]", "").Replace("", "").Replace("", "")


Regex.Replace(match.Groups["special"].Value, @"[]+", "")



Try following :


string input = "Text 1^%+TAB(CMD 1CMD 2)Text 2.^(abc)";
string pattern = @"^(?'text1'[^^]+)(?'special1'[^(]+)(?'special2'[^)]+))(?'text2'[^^]+)(?'special3'.*)";

Match match = Regex.Match(input, pattern);
Console.WriteLine("Text : '0' Special : '1' Special : '2' Text : '3' Special : '4'",
match.Groups["text1"].Value,
match.Groups["special1"].Value,
match.Groups["special2"].Value,
match.Groups["text2"].Value,
match.Groups["special3"].Value
);
Console.ReadLine();



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế