Why does `(c*)|(cccd)` match `ccc`, not `cccd`?
NickName:Alex Chamberlain Ask DateTime:2015-03-22T02:06:41

Why does `(c*)|(cccd)` match `ccc`, not `cccd`?

I thought I understood Regular Expressions pretty well, but why is this matching 'ccc', not 'cccd'?

>>> mo = re.match('(c*)|(cccd)', 'cccd')
>>> mo.group(0)
'ccc'

This particular case is using Python's re module.

Copyright Notice:Content Author:「Alex Chamberlain」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/29186169/why-does-ccccd-match-ccc-not-cccd

Answers
Jason B 2015-03-21T18:09:53

Your regex ((c*)|(cccd)) is saying match either one of two things:\n\n\n0 or unlimited c's\nThe literal sequence cccd\n\n\nBecause regular expressions are greedy, it consumes the ccc string as the match, so that is what you're returning. It will first try what ever comes first (in this case c*, and if it is able to make a match, then it will.\n\nTo correct to what you want, try the regex: (cccd)|(c*). With this:\n\n>>> mo = re.match('(cccd)|(c*)', 'cccd')\n>>> mo.group(0)\n'cccd'\n\n\nExample is here: https://regex101.com/r/aU8pE7/1",


Avinash Raj 2015-03-21T18:11:33

Regex patterns are evaluated from left to right. Put the pattern which has higher precedence as first (to the left of |) and the lower precedence as second (to the right of |). Note that the second pattern was not allowed to match the text which was already matched by the first pattern. That is, regex engine by default won't do overlapping matches. To make the regex engine to do overlapping match then you need to put your pattern inside a capturing group and again put the capturing group inside a positive lookaround assertion (positive lookahead and positive lookbehind).\n\nmo = re.match('(cccd)|(c*)', 'cccd')\n",


More about “Why does `(c*)|(cccd)` match `ccc`, not `cccd`?” related questions

Why does `(c*)|(cccd)` match `ccc`, not `cccd`?

I thought I understood Regular Expressions pretty well, but why is this matching 'ccc', not 'cccd'? >>> mo = re.match('(c*)|(cccd)', 'cccd') >>> mo.group(0)

Show Detail

Why does `(c*)|(cccd)` match `ccc`, not `cccd`?

I thought I understood Regular Expressions pretty well, but why is this matching 'ccc', not 'cccd'? >>> mo = re.match('(c*)|(cccd)', 'cccd') >>> mo.group(0)

Show Detail

Windows 10 BLE C#: Subscribing to notifications from the peripheral using cccd fails first time for non-bonded devices

I am trying to develop a WPF app on windows 10 to communicate to a BLE device. I am using advertisement watcher class to scan for new devices. I am expected to authenticate (proprietary) to the dev...

Show Detail

Why does setCharacteristicNotification() not actually enable notifications?

The BluetoothLeGatt Android BLE example contains the following code: public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,

Show Detail

What android API to call in order to write "command" to CCCD handle in BLE?

Purpose: Write "commands"(quoted because I haven't figure out the right command, I was told it's 0x0001, 0x0010, 0x01, 0x02, etc ) to CCCD handle on the BLE stack to enable notification/indication ...

Show Detail

Is the relative neighborhood graph of the cccd library of R working properly or am I doing something wrong?

I have loaded a 2D dataset and I wanted to try some graph-based clustering algorithms, however the relative neighborhood graph I get seems to be missing a lot of edges. Here is the result I get, an...

Show Detail

flutter_blue set_notification_error, could not locate CCCD descriptor for characteristic

I created a flutter demo app for BLE connection and communication. I'm try listen the data from characteristic value. But doing the await characteristic.setNotifyValue(true); will cause an error. The

Show Detail

Why my Google Drive Refresh Token is expiring after 7 days?

I implemented a Laravel web-app with Google Drive's API used for storage. I used this tutorial https://gist.github.com/sergomet/f234cc7a8351352170eb547cccd65011. I note that in my application "

Show Detail

How to get query to return rows where first three characters of one row match another row?

Here's my data: with first_three as ( select 'AAAA' as code from dual union all select 'BBBA' as code from dual union all select 'BBBB' as code from dual union all

Show Detail

Flutter_blue and Adafruit Bluefruit BLE characteristic error: could not locate CCCD descriptor for characteristic

I am currently running into an issue with my ble project for flutter. I am using the flutter blue package by Paul DeMarco and the additional page for the app is based on "ThatProject" dust

Show Detail