Tarun. P
Tarun. P

Reputation: 442

Regex with multiple repetation

I have difficult time with regex right now.

I am able to debug the regex in Regexbuddy but somehow it is not working in C#.net.

For example.

My Input string:-

--8W1_805_431____0210_A01_0002_L--;%{PE}%R  
8.2.24,%MKUKATPBASIS,%CCOMMENT,%VNORMAL,%P 2: --8W1_805_431____0210_A01_0002_L--
;ENDFOLD  

;FOLD PTP SG003920 Vel=100 % PDAT5 ServoGun=8 Cont=OPN Part=2.65 mm WeldTimer=WTDAT1 Tool[1]:zc1 Base[1]:510wz1;%{PE}%R 4.1.25,%MKUKATPSERVOTECH,%CSpotTC,%VPTP,%P 1:PTP, 2:SG003920, 3:, 5:100, 7:PDAT5, 9:1, 11:1, 13:OPN, 15:2.65, 18:1, 21:0, 24:0, 27:5, 30:0, 33:WTDAT1
$BWDSTART=FALSE
PDAT_ACT=PPDAT5
FDAT_ACT=FSG003920
BAS(#PTP_PARAMS,100)
EG_SPOT_POINT_NAME[]="SG003920"
EG_EXTAX_ACTIVE=EG_SERVOGUN_EXAXIS[1]
EG_THICKNESS=2.65
EG_GUN_FORCE=1
EG_COMP_HELP=0
EG_COMPENSATE_PATH=0
EG_PROGRAM_NUMBER=003920
EG_HELPPOINT=EG_CHTIP(XSG003920)
PTP EG_HELPPOINT 
EG_APPROX_OPEN=FALSE
CMD=#SPOT
EG_SPOT_POINT=EG_HELPPOINT
EG_SPOT( WTWTDAT1 )  
PTP EG_HELPPOINT C_DIS
CMD=#STOP_POINT_END
EG_SPOT_POINT=EG_HELPPOINT
EG_SPOT( )

  ; --8W1_112_431____0210_A01_0001_L--;%{PE}%R 8.2

;FOLD PTP SG003918 Vel=100 % PDAT7 ServoGun=1 Cont=OPN Part=2.65 mm WeldTimer=WTDAT2 Tool[1]:zc1 Base[1]:510wz1;%{PE}%R 4.1.25,%MKUKATPSERVOTECH,%CSpotTC,%VPTP,%P 1:PTP, 2:SG003918, 3:, 5:100, 7:PDAT7, 9:1, 11:1, 13:OPN, 15:2.65, 18:1, 21:0, 24:0, 27:5, 30:0, 33:WTDAT2
$BWDSTART=FALSE
PDAT_ACT=PPDAT7
FDAT_ACT=FSG003918
BAS(#PTP_PARAMS,100)
EG_SPOT_POINT_NAME[]="SG003918"
EG_EXTAX_ACTIVE=EG_SERVOGUN_EXAXIS[1]
EG_THICKNESS=2.65
EG_GUN_FORCE=1
EG_COMP_HELP=0
EG_COMPENSATE_PATH=0
EG_PROGRAM_NUMBER=003918
EG_HELPPOINT=EG_CHTIP(XSG003918)
PTP EG_HELPPOINT 
EG_APPROX_OPEN=FALSE
CMD=#SPOT
EG_SPOT_POINT=EG_HELPPOINT
EG_SPOT( WTWTDAT2 )
PTP EG_HELPPOINT C_DIS
CMD=#STOP_POINT_END
EG_SPOT_POINT=EG_HELPPOINT
EG_SPOT( )
;ENDFOLD


Regex Pattern:- --(?<PName>8W.*?)--.*?(?<MType>PTP|LIN).*?(?<PointNa>SG) 

The output has to be like this for the group PName.

8W1_112_431____0210_A01_0001_L

8W1_805_431____0210_A01_0002_L

As I run this regex in c# its returns always false.

Please any one guide me what is wrong over here.

Thanks in advance.

Upvotes: 1

Views: 77

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627469

The solution is easy: add the (?s) at the beginning. Or use RegexOptions.Singleline flag with your Regex.Match.

See IDEONE demo

You need to understand the role of regex modifiers in .NET regex:

  • RegexOptions.Singleline - Specifies single-line mode. Changes the meaning of the dot (.) so it matches every character (instead of every character except \n). For more information, see the "Single-line Mode" section in the Regular Expression Options topic.

  • RegexOptions.Multiline - Multiline mode. Changes the meaning of ^ and $ so they match at the beginning and end, respectively, of any line, and not just the beginning and end of the entire string. For more information, see the "Single-line Mode" section in the Regular Expression Options topic.

Also, see Difference between regular expression modifiers 'm' and 's'?

Upvotes: 1

Related Questions