简体   繁体   English

正则表达式匹配组

[英]Regex to match groups

main:
  lda #$5
  sta $2
  lda #$0
  sta $0  
  lda #$10    
  sta $1 

I'm looking for a regex that will match a Group like this: ["lda", "#$", "5"]. 我正在寻找一个正则表达式,将匹配这样的组:[“ lda”,“#$”,“ 5”]。 I've tried looking at tutorials for hours and it's still so very confusing to me. 我尝试看了几个小时的教程,但对我来说仍然很困惑。

Here's what I have attempted so far: 到目前为止,这是我尝试过的事情:

^(?<label>.*?)\s*(?<sign>[#][$])\s*(?<num>\d)$

It seems to match up only lda #$5 似乎只匹配lda#$ 5

This is all in Visual Studios 2012 C# 这一切在Visual Studios 2012 C#中

(\\w+)\\s+((#\\$)(\\d+)|\\$\\d+)应该这样做: http : //regex101.com/r/pJ0yH8

You can use this regex: 您可以使用此正则表达式:

(?<=^\s*)(\w+)\s(\D+)(\d)

The code would be like: 代码如下:

String input = "  lda #$5";
String pattern = @"(?<=^\s*)(\w+)\s(\D+)(\d+)$";

Match m = Regex.Match(input, pattern, RegexOptions.Multiline);

if (m.Groups.Count == 4) 
{
    string firstpart = m.Groups[1].ToString();  // lda
    string secondpart = m.Groups[2].ToString();  // #$
    string thirdpart = m.Groups[3].ToString();  // 5
}

Try this : 尝试这个 :

(\w+)\s+([^\d]+)(\d+)

above works for all the input you have provided. 以上内容适用于您提供的所有输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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