简体   繁体   中英

How to use regex to find exact match of a word but ignoring the same text with preposition? C#

hi i've been working with DXF files and i got some trouble for regular expression. i have some text like this

   BODY
   123
   abc
   GR-BODY
   attrib
   AcdbLine

and i've write some regular expression that should be work but clearly i still need some help for this regular expression

here is my code

string[] tmp = Regex.Split(originalString, @"(3DFACE|3DSOLID|ACAD_PROXY_ENTITIY|ARC|ATTDEF|ATTRIB|BODY|CIRCLE|DIMENSION|ELLIPSE|HATCH|HELIX|IMAGE|INSERT|LEADER|LIGHT|LWPOLYLINE|MLINE|MLEADERSTYLE|MLEADER|MTEXT|OLEFRAME|OLE2FRAME|POINT|POLYLINE|RAY|REGION|SEQEND|SHAPE|SOLID|SPLINE|SUN|SURFACE|TABLE|TEXT|TOLERANCE|TRACE|UNDERLAY|VERTEX|VIEWPORT|WIPEOUT|XLINE|LINE)", RegexOptions.None);

and i would like just to catch the BODY text but the GR-BODY still included, how to exclude the GR-BODY? thanks

EDIT 1 i'm sorry i look for the wrong code earlier

umm i want to the output like this

tmp[0] = BODY
tmp[1] = 123\nabc\nGR-LINE\nattrib\nAcdbLine

since my code only been able to make it like this

tmp[0] = BODY
tmp[1] = 123\nabc\nGR-
tmp[2] = BODY\nattrib\nAcdbLine

That regex statement should work. Try using Regex.Matches to return a MatchCollection instead.

   MatchCollection mc = Regex.Matches(originalString, @"(3DFACE|3DSOLID|ACAD_PROXY_ENTITIY|ARC|ATTDEF|ATTRIB|BODY|CIRCLE|DIMENSION|ELLIPSE|HATCH|HELIX|IMAGE|INSERT|LEADER|LIGHT|LWPOLYLINE|MLINE|MLEADERSTYLE|MLEADER|MTEXT|OLEFRAME|OLE2FRAME|POINT|POLYLINE|RAY|REGION|SEQEND|SHAPE|SOLID|SPLINE|SUN|SURFACE|TABLE|TEXT|TOLERANCE|TRACE|UNDERLAY|VERTEX|VIEWPORT|WIPEOUT|XLINE|LINE)", RegexOptions.None);
   string[] tmp = mc.Cast<Match>().Select(m => m.Value).ToArray();

If your words are always from the start to the end of the row, then tell the pattern this:

string[] tmp = Regex.Split(originalString, @"^(3DFACE|3DSOLID|ACAD_PROXY_ENTITIY|ARC|ATTDEF|ATTRIB|BODY|CIRCLE|DIMENSION|ELLIPSE|HATCH|HELIX|IMAGE|INSERT|LEADER|LIGHT|LWPOLYLINE|MLINE|MLEADERSTYLE|MLEADER|MTEXT|OLEFRAME|OLE2FRAME|POINT|POLYLINE|RAY|REGION|SEQEND|SHAPE|SOLID|SPLINE|SUN|SURFACE|TABLE|TEXT|TOLERANCE|TRACE|UNDERLAY|VERTEX|VIEWPORT|WIPEOUT|XLINE|LINE)$", RegexOptions.Multiline);

This should give you the output you expect.

^ Matches the start of the row when the Multiline option is used

$ Matches the end of the row when the Multiline option is used

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