Regext仅匹配模式的一次出现

时间:2015-07-09 04:37:56

标签: regex linux

如何编写一个正则表达式,表明给定的模式只出现一次?

例如..如果我正在搜索模式'文件',

      123file345 --> Match
      asdffile12 --> match
      file12file --> does not match

3 个答案:

答案 0 :(得分:2)

            // get the layout inflater

            LayoutInflater mLayoutInflater = (LayoutInflater) getApplicationContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            // create a ViewHolder reference
            ViewHolder holder = null;

            // check to see if the reused view is null or not, if is not
            // null then reuse it

            if (view == null) {
                holder = new ViewHolder();
                // check to see if the reused view is null or not, if is not
                // null then reuse it
                if (view == null) {
                    holder = new ViewHolder();

                    view = mLayoutInflater.inflate(
                            R.layout.listview_values, null);
                    holder.txt_date = (TextView) view
                            .findViewById(R.id.t_date);
                    holder.txt_intime = (TextView) view
                            .findViewById(R.id.txt_intime);
                    holder.txt_outtime = (TextView) view
                            .findViewById(R.id.txt_outtime);

                    // the setTag is used to store the data within this view
                    view.setTag(holder);
                } else {
                    // the getTag returns the viewHolder object set as a tag
                    // to the view
                    holder = (ViewHolder) view.getTag();
                    view = mLayoutInflater.inflate(
                            R.layout.listview_values, parent, false);
                }

            }
            // get the string item from the position "position" from array
            // list to put it on the TextView
            String date = attendence_webdata.get(position).get("A_date")
                    .toString();
            String intime = attendence_webdata.get(position)
                    .get("Ain_time").toString();
            String outtime = attendence_webdata.get(position)
                    .get("out_time").toString();
            if (date != null) {
                if (holder.txt_date != null) {
                    // set the item name on the TextView
                    holder.txt_date.setText(date);
                }
            }
            if (intime != null) {
                if (holder.txt_intime != null) {
                    // set the item name on the TextView
                    holder.txt_intime.setText(intime);
                }
            }
            if (outtime != null) {
                if (holder.txt_outtime != null) {
                    // set the item name on the TextView
                    holder.txt_outtime.setText(outtime);
                }
            }

            // this method must return the view corresponding to the data at
            // the specified position.
            return view;

        }
    }

}

/**
 * Static class used to avoid the calling of "findViewById" every time the
 * getView() method is called, because this can impact to your application
 * performance when your list is too big. The class is static so it cache
 * all the things inside once it's created.
 */
private static class ViewHolder {
    private TextView txt_outtime;
    private TextView txt_intime;
    private TextView txt_date;

}

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();
    attendence_webdata.clear();// where attendence_webdata is arraylistname
    list.setAdapter(null);     //where list is your listview name
}

您可以将其与^(?:(?!file).)*file(?:(?!file).)*$ 一起使用。请参阅演示。

https://regex101.com/r/cK4iV0/30

答案 1 :(得分:2)

您可以使用否定lookahead

@asyncio.coroutine
def init(loop):
    return (yield from loop.create_server(web.Application().make_handler(), '0.0.0.0', 8080))

Test at regex101

您可以在grep。

中应用上述正则表达式
^(?!(?:.*?file){2}).*?file.*$

grep -P '^(?!(?:.*?file){2}).*?file' file 开始^的否定前瞻声明展望未来,如果不是2x((?!(?:.*?file){2}) lazily任何数量的任何字符,后跟子串.*?)。
如果这个条件成功,它匹配file所有字符串,如果它包含子串.*?file.*

如果只是匹配该行,则不需要最后一个贪婪点(以匹配file之后的任何字符)。

enter image description here

答案 2 :(得分:0)

您只需要使用grep:

的反转选项
grep -v 'file.*file' file