正则表达式与“AND”opeartor结合

时间:2018-01-10 16:24:26

标签: regex regex-group

我想用AND运算符连接两个正则表达式(我不知道这是否可能......)所以匹配只在字符串与RegEx1和RegEx2匹配时才会发生。

private void printDocument1_PrintPage(object sender, PrintPageEventArgs e){
  Graphics graphics = e.Graphics;
  int ypos = 78;
  Font f1 = new Font("Arial", 14, FontStyle.Bold, GraphicsUnit.Pixel);
  Brush brush = new SolidBrush(Color.LightSlateGray);
  graphics.FillRectangle(brush, new Rectangle(10, 10, 770, 50));
  Pen blackpen = new Pen(Color.Black);
  Pen graypen = new Pen(Color.LightGray);
  Pen redpen = new Pen(Color.Red);
  graphics.DrawRectangle(blackpen, new Rectangle(10, 10, 770, 50));
  Brush B = new SolidBrush(listView1.ForeColor);

  graphics.DrawLine(blackpen, 10, 10, 10, 1132);
  graphics.DrawString("FORENAME", f1, Brushes.Black, new Point(20, 25));
  graphics.DrawLine(blackpen, 130, 10, 130, 1132);
  graphics.DrawString("SURNAME", f1, Brushes.Black, new Point(140, 25));
  graphics.DrawLine(blackpen, 290, 10, 290, 1132);
  graphics.DrawString("EXT.", f1, Brushes.Black, new Point(300, 25));
  graphics.DrawLine(blackpen, 380, 10, 380, 1132);
  graphics.DrawString("JOB TITLE", f1, Brushes.Black, new Point(410, 25));
  graphics.DrawLine(blackpen, 780, 10, 780, 1132);

  int[] X = { 15, 140, 300, 390, 720 }; 
  int Y = 60; 
  f1 = listView1.Font;

  for (int I = 0; I < listView1.Items.Count; I++){
     for (int J = 0; J < listView1.Items[I].SubItems.Count - 1; J++){
        graphics.DrawString(listView1.Items[I].SubItems[J].Text, f1, B, X[J], Y);
     }
     Y += f1.Height;
     graphics.DrawLine(graypen, 10, ypos, 780, ypos);
     ypos = ypos + 17;

     if (ypos > 1132){
       e.HasMorePages = true;
       return;                   
     }
   }
   graphics.Dispose();
 }

使用OR运算符很容易,但对于AND我找不到解决方案。

2 个答案:

答案 0 :(得分:1)

使用积极的前瞻结合&#39;字符串的开头和结尾&#39;锚点,以确保整个字符串匹配,没有&#34;非法&#34;信件存在:

^(?=[a-g]+$)(?=[b-z]+$).*

https://regex101.com/r/Bz7qnb/2/

答案 1 :(得分:0)

您可以使用正向前瞻操作符(?=)来组合表达式:

(?=[a-g]+)(?=[b-z]+)

这是测试它的小提琴:https://regex101.com/r/kyy6XZ/1

在这种情况下,它在逻辑上等同于[b-g]+,这意味着它应匹配bg区间内具有字母或更多字母的任何字符串,边界包括在内。