正则表达式过滤掉以“字符串”后缀结尾的单词 - 没有预测

时间:2018-04-07 19:02:02

标签: ruby regex gitlab re2

我正在尝试提供一个与以下字符串匹配的Ruby Regex:

MAINT: Refactor something
STRY-1: Add something
STRY-2: Update something

但不应与以下内容相符:

MAINT: Refactored something
STRY-1: Added something
STRY-2: Updated something

MAINT: Refactoring something
STRY-3: Adding something
STRY-4: Updating something

基本上,后面的第一个单词:不应该以ed或ing结尾

我暂时使用以下正则表达式GitLab提交消息。

^(MAINT|(STRY|PRB)-\d+):\s(?:(?!(?:ed|ing)\b)[A-Za-z])+\s([a-zA-Z0-9._\-"].*)

但是,最近他们似乎已经转而使用不支持前瞻的google/re2

是否可以重新编写此正则表达式以便不使用前瞻?

3 个答案:

答案 0 :(得分:1)

str =<<_
MAINT: Refactor something
STRY-1: Added something
MAINT: Refactoring something
Add something
STRY-3: Adding something
STRY-1: Add something
MAINT: Refactored something
Refactor something
STRY-4: Updating something
STRY-9:   Update something
STRY-2: Updated something
_

r = /
    ^                      # Match beginning of line
    (?:                    # Begin non-capture group
      MAINT\:[ ]+Refactor  # Match string
      |                    # or
      STRY-\d+\:[ ]+       # match string
      (?:Add|Update)       # match 'Add' or 'Update'
    )                      # end non-capture group
    [ ]+something          # match one or more spaces followed by 'something'
    $                      # match end of line
    /x                     # free-spacing regex definition modes

str.scan(r)
  #=> ["MAINT: Refactor something\n",
  #    "STRY-1: Add something\n",
  #    "STRY-9:   Update something\n"]

要匹配正则表达式中的空格,我使用包含空格([ ])的字符类。这是必需的,因为自由间隔模式会删除不在字符类中的空格。以常规方式编写,正则表达式如下。

/^(?:MAINT\: +Refactor|STRY-\d+\: +(?:Add|Update)) +something$/

答案 1 :(得分:1)

你正在处理一个必须要注意三个结尾的正则表达式:

  1. ed\b
  2. ing\b
  3. ied\b
  4. 你必须考虑每个单一地点的存在。例如,e[^d]\b[^e]d\b。写下所有这些你将得到这个正则表达式:

    ^(MAINT|(STRY|PRB)-\d+):\s*(?i:\w*(e[a-ce-z]|[a-df-z]d|i(n[a-fh-z]|[a-mo-z]g|e[a-ce-z]|[a-df-z]d)|[a-hj-z]ng|[a-hj-z][a-df-mo-z][a-cefh-z])|\w)\s([a-zA-Z0-9._\-"].*)
    

    Live demo

答案 2 :(得分:-1)

没有正则表达式:

/***** script for getting live feed from camera *****/

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CamScript : MonoBehaviour {

    private bool camAvilable ;
    private WebCamTexture webcam;
    private Texture2D defaultbackground;
    private Texture2D output;

    public RawImage background;
    public RawImage imageToDisplay;
    public AspectRatioFitter fit;
    private Color[] data;

    void IntialCam(){
        defaultbackground = background.texture;

        WebCamDevice[] cam_devices = WebCamTexture.devices;

        if(cam_devices.Length == 0){
            Debug.Log ("No Camera");
            camAvilable = false;
            return;
        }

        webcam = new WebCamTexture (cam_devices [0].name, 320, 400, 60);

        if (webcam == null) {
            Debug.Log ("Unable to detect camera");
            return;
        }
        webcam.Play ();
        background.texture = webcam;
        imageToDisplay.texture = output;
        camAvilable = true;
    }

    void GetFeed(){
        if(!camAvilable){
            return;
        }

        float ratio = (float)webcam.width / (float)webcam.height;
        fit.aspectRatio = ratio;
        float scaleY = webcam.videoVerticallyMirrored ? -1f : 1f;
        background.rectTransform.localScale = new Vector3 (1f, scaleY, 1f);
        int orinat = -webcam.videoRotationAngle;
        background.rectTransform.localEulerAngles = new Vector3 (0,0,orinat);
    }

    void Start () {

        IntialCam ();

        output = new Texture2D (320,400);
        GetComponent<Renderer> ().material.mainTexture = output; 
        data = new Color[webcam.width * webcam.height];
    }

    // Update is called once per frame
    void Update () {
        GetFeed ();
        webcam.GetPixels(data);
        output.SetPixels(data);
        output.Apply();
    }

}