本文介绍了多repetation正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有困难的时候与现在正则表达式。



我能够调试使用RegexBuddy正则表达式,但不知它是不是在C#.NET工作。



例如



我的输入字符串: -

   -  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威赛= 100%PDAT5 ServoGun = 8 CONT = OPN部分=2.65毫米WeldTimer = WTDAT1工具[1]:ZC1基地[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威赛= 100%PDAT7 ServoGun = 1 CONT = OPN部分=2.65毫米WeldTimer = WTDAT2工具[1]:ZC1基地[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()$ ?b $ b:ENDFOLD


正则表达式: - - (?。?< PNAME> 8W *) - *(?< M型> PTP | LIN)。 *?(小于?PointNa> SG)

输出已经是这样的组PNAME



8W1_112_431 ____ 0210_A01_0001_L



8W1_805_431 ____ 0210_A01_0002_L



在我运行这个正则表达式在C#中的收益始终为false。



请任何一个指导我什么是错在这里。



在此先感谢


解决方案

解决的办法很简单:添加 开头。或者使用 RegexOptions.Singleline 标记与 Regex.Match



请参阅



您需要了解在正则表达式的修饰符.NET正则表达式:




  • RegexOptions.Singleline - 指定单行模式。改变点的含义(),因此它的每一个字符匹配(而不是每个字符,除了 \\\
    ) 。欲了解更多信息,请参阅的话题。


  • RegexOptions.Multiline - 多行模式。更改的含义 ^ $ ,使他们分别匹配的开始和结束,任何线路,而不是刚开始的时候,整个字符串的结尾。欲了解更多信息,请参阅的话题。




另外,看到的 Difference >

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.

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'?

这篇关于多repetation正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 12:46