C#修剪mac地址正则表达式匹配文本文件

时间:2015-08-06 02:33:51

标签: c# regex

如何从多厂商交换机mac-address表转储中修剪所有有效的mac地址?

到目前为止,我已经得到了以下内容......

public class LinearLayoutBehavior extends CoordinatorLayout.Behavior<LinearLayout> {
    private static final Interpolator INTERPOLATOR = new FastOutSlowInInterpolator();
    private boolean mIsAnimatingOut = false;

    public LinearLayoutBehavior(Context context, AttributeSet attrs) {
        super();
    }

    @Override
    public boolean onStartNestedScroll(final CoordinatorLayout coordinatorLayout, final LinearLayout child,
                                       final View directTargetChild, final View target, final int nestedScrollAxes) {
        return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL
                || super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, nestedScrollAxes);
    }

    @Override
    public void onNestedScroll(final CoordinatorLayout coordinatorLayout, final LinearLayout child,
                               final View target, final int dxConsumed, final int dyConsumed,
                               final int dxUnconsumed, final int dyUnconsumed) {
        super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);
        if (dyConsumed > 0 && !this.mIsAnimatingOut && child.getVisibility() == View.VISIBLE) {
            // User scrolled down and the FAB is currently visible -> hide the FAB
            animateOut(child);
        } else if (dyConsumed < 0 && child.getVisibility() != View.VISIBLE) {
            // User scrolled up and the FAB is currently not visible -> show the FAB
            animateIn(child);
        }
    }

    private void animateOut(final LinearLayout linearLayout) {
        if (Build.VERSION.SDK_INT >= 14) {
            ViewCompat.animate(linearLayout).translationY(168F).alpha(0.0F).setInterpolator(INTERPOLATOR).withLayer()
                    .setListener(new ViewPropertyAnimatorListener() {
                        public void onAnimationStart(View view) {
                            LinearLayoutBehavior.this.mIsAnimatingOut = true;
                        }

                        public void onAnimationCancel(View view) {
                            LinearLayoutBehavior.this.mIsAnimatingOut = false;
                        }

                        public void onAnimationEnd(View view) {
                            LinearLayoutBehavior.this.mIsAnimatingOut = false;
                            view.setVisibility(View.GONE);
                        }
                    }).start();
        } else {
            Animation anim = AnimationUtils.loadAnimation(linearLayout.getContext(), R.anim.fab_out);
            anim.setInterpolator(INTERPOLATOR);
            anim.setDuration(200L);
            anim.setAnimationListener(new Animation.AnimationListener() {
                public void onAnimationStart(Animation animation) {
                    LinearLayoutBehavior.this.mIsAnimatingOut = true;
                }

                public void onAnimationEnd(Animation animation) {
                    LinearLayoutBehavior.this.mIsAnimatingOut = false;
                    linearLayout.setVisibility(View.GONE);
                }

                @Override
                public void onAnimationRepeat(final Animation animation) {
                }
            });
            linearLayout.startAnimation(anim);
        }
    }

    private void animateIn(LinearLayout linearLayout) {
        linearLayout.setVisibility(View.VISIBLE);
        if (Build.VERSION.SDK_INT >= 14) {
            ViewCompat.animate(linearLayout).translationY(0).scaleX(1.0F).scaleY(1.0F).alpha(1.0F)
                    .setInterpolator(INTERPOLATOR).withLayer().setListener(null)
                    .start();
        } else {
            Animation anim = AnimationUtils.loadAnimation(linearLayout.getContext(), R.anim.fab_in);
            anim.setDuration(200L);
            anim.setInterpolator(INTERPOLATOR);
            linearLayout.startAnimation(anim);
        }
    }
}

输入行在MAC地址之前或之后不一定具有相同数量的字符。使用输入文件,例如:

        string strLineBuf = "Cisco 10 001c.aabb.ccdd Gi0/50";
        // string strLineBuf = "HP 001caa-bbccdd 50 10";
        // string strLineBuf = "Other 00:1c:aa:bb:cc:dd 50";

        // finds valid MAC addresses with space before and after
        Regex rex = new Regex(@"^.* ([0-9A-F]{2}[:.-]?){5}[0-9A-F]{2} .*$", RegexOptions.IgnoreCase);
        Match m = rex.Match(strLineBuf);

        if (m.Success)
        {
            Console.WriteLine("Valid MAC found in line :)");
            // trim MAC from line
            // save line to arraylist
        }

我想输出:

Cisco 10 001c.aabb.ccdd Gi0/50
HP 001caa-bbccdd 50 10
Other 00:1c:aa:bb:cc:dd 50

2 个答案:

答案 0 :(得分:1)

你可能不得不调整Mac模式,但是12最小似乎是合理的提示,要求解析器使其工作。

var data = @"Cisco 10 001c.aabb.ccdd Gi0/50
HP 001caa-bbccdd 50 10
Other 00:1c:aa:bb:cc:dd 50";

var pattern =
@"^
   (?<Brand>.+?)
   (?:\s+)
   (?<MAC>[\w.:\-]{12,})
   (?:\s+)
   (?<Data>[^\r\n]+)";

Regex.Matches(data, pattern, RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline)
     .OfType<Match>()
     .Select (mt => new
                    {
                       Brand = mt.Groups["Brand"].Value,
                       MAC   = mt.Groups["MAC"].Value,
                       Data  = mt.Groups["Data"].Value
                    }
             )

结果动态实体

enter image description here

答案 1 :(得分:1)

你的模式已经匹配了你的样本数据,所以我只是调整实际上将整个mac地址匹配到一个捕获组中,这样就可以用String.Replace()来删除它。

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

public class Program
{
    public static void Main()
    {
        List<string> data = new List<string>()
        {
            "Cisco 10 001c.aabb.ccdd Gi0/50",
            "HP 001caa-bbccdd 50 10",
            "Other 00:1c:aa:bb:cc:dd 50"
        };

        foreach (string strLineBuf in data)
        {
            // Finds valid MAC addresses with space before and after
            Regex rex = new Regex(@"^.* (([0-9A-F]{2}[:.-]?){5}[0-9A-F]{2}\s?).*$", RegexOptions.IgnoreCase);
            Match m = rex.Match(strLineBuf);
            if (m.Success)
            {
                Console.WriteLine(strLineBuf.Replace(m.Groups[1].Value, String.Empty));
            }
        }
    }
}

结果:

Cisco 10 Gi0/50
HP 50 10
Other 50

Fiddle Demo