Rectangle 27 141

Addendum: What's with the (?<A-B>...) syntax?

As far as I know, balancing groups are unique to .NET's regex flavor.

First, you need to know that .NET is (again, as far as I know) the only regex flavor that lets you access multiple captures of a single capturing group (not in backreferences but after the match has completed).

To illustrate this with an example, consider the pattern

(.)+

and the string "abcd".

in all other regex flavors, capturing group 1 will simply yield one result: d (note, the full match will of course be abcd as expected). This is because every new use of the capturing group overwrites the previous capture.

.NET on the other hand remembers them all. And it does so in a stack. After matching the above regex like

Match m = new Regex(@"(.)+").Match("abcd");
m.Groups[1].Captures

Is a CaptureCollection whose elements correspond to the four captures

0: "a"
1: "b"
2: "c"
3: "d"

where the number is the index into the CaptureCollection. So basically every time the group is used again, a new capture is pushed onto the stack.

It gets more interesting if we are using named capturing groups. Because .NET allows repeated use of the same name we could write a regex like

(?<word>\w+)\W+(?<word>\w+)

to capture two words into the same group. Again, every time a group with a certain name is encountered, a capture is pushed onto its stack. So applying this regex to the input "foo bar" and inspecting

m.Groups["word"].Captures
0: "foo"
1: "bar"

This allows us to even push things onto a single stack from different parts of the expression. But still, this is just .NET's feature of being able to track multiple captures which are listed in this CaptureCollection. But I said, this collection is a stack. So can we pop things from it?

It turns out we can. If we use a group like (?<-word>...), then the last capture is popped from the stack word if the subexpression ... matches. So if we change our previous expression to

(?<word>\w+)\W+(?<-word>\w+)

Then the second group will pop the first group's capture, and we will receive an empty CaptureCollection in the end. Of course, this example is pretty useless.

But there's one more detail to the minus-syntax: if the stack is already empty, the group fails (regardless of its subpattern). We can leverage this behavior to count nesting levels - and this is where the name balancing group comes from (and where it gets interesting). Say we want to match strings that are correctly parenthesized. We push each opening parenthesis on the stack, and pop one capture for each closing parenthesis. If we encounter one closing parenthesis too many, it will try to pop an empty stack and cause the pattern to fail:

So we have three alternatives in a repetition. The first alternative consumes everything that is not a parenthesis. The second alternative matches (s while pushing them onto the stack. The third alternative matches )s while popping elements from the stack (if possible!).

(?=.*[(])

This pattern is not perfect (or entirely correct) though.

There is one more catch: this does not ensure that the stack is empty at the end of the string (hence (foo(bar) would be valid). .NET (and many other flavors) have one more construct that helps us out here: conditional patterns. The general syntax is

(?(condition)truePattern|falsePattern)

where the falsePattern is optional - if it is omitted the false-case will always match. The condition can either be a pattern, or the name of a capturing group. I'll focus on the latter case here. If it's the name of a capturing group, then truePattern is used if and only if the capture stack for that particular group is not empty. That is, a conditional pattern like (?(name)yes|no) reads "if name has matched and captured something (that is still on the stack), use pattern yes otherwise use pattern no".

So at the end of our above pattern we could add something like (?(Open)failPattern) which causes the entire pattern to fail, if the Open-stack is not empty. The simplest thing to make the pattern unconditionally fail is (?!) (an empty negative lookahead). So we have our final pattern:

^(?:[^()]|(?<Open>[(])|(?<-Open>[)]))*(?(Open)(?!))$

Note that this conditional syntax has nothing per se to do with balancing groups but it's necessary to harness their full power.

From here, the sky is the limit. Many very sophisticated uses are possible and there are some gotchas when used in combination with other .NET-Regex features like variable-length lookbehinds (which I had to learn the hard way myself). The main question however is always: is your code still maintainable when using these features? You need to document it really well, and be sure that everyone who works on it is also aware of these features. Otherwise you might be better off, just walking the string manually character-by-character and counting nesting levels in an integer.

Now with all of the above, we can validate that a string is correctly parenthesized. But it would be a lot more useful, if we could actually get (nested) captures for all those parentheses' contents. Of course, we could remember opening and closing parentheses in a separate capture stack that is not emptied, and then do some substring extraction based on their positions in a separate step.

But .NET provides one more convenience feature here: if we use (?<A-B>subPattern), not only is a capture popped from stack B, but also everything between that popped capture of B and this current group is pushed onto stack A. So if we use a group like this for the closing parentheses, while popping nesting levels from our stack, we can also push the pair's content onto another stack:

^(?:[^()]|(?<Open>[(])|(?<Content-Open>[)]))*(?(Open)(?!))$

So taking all of these things together we can:

All in a single regular expression. If that's not exciting... ;)

Some resources that I found helpful when I first learned about them:

c# - What are regular expression Balancing Groups? - Stack Overflow

c# .net regex balancing-groups
Rectangle 27 140

Addendum: What's with the (?<A-B>...) syntax?

As far as I know, balancing groups are unique to .NET's regex flavor.

First, you need to know that .NET is (again, as far as I know) the only regex flavor that lets you access multiple captures of a single capturing group (not in backreferences but after the match has completed).

To illustrate this with an example, consider the pattern

(.)+

and the string "abcd".

in all other regex flavors, capturing group 1 will simply yield one result: d (note, the full match will of course be abcd as expected). This is because every new use of the capturing group overwrites the previous capture.

.NET on the other hand remembers them all. And it does so in a stack. After matching the above regex like

Match m = new Regex(@"(.)+").Match("abcd");
m.Groups[1].Captures

Is a CaptureCollection whose elements correspond to the four captures

0: "a"
1: "b"
2: "c"
3: "d"

where the number is the index into the CaptureCollection. So basically every time the group is used again, a new capture is pushed onto the stack.

It gets more interesting if we are using named capturing groups. Because .NET allows repeated use of the same name we could write a regex like

(?<word>\w+)\W+(?<word>\w+)

to capture two words into the same group. Again, every time a group with a certain name is encountered, a capture is pushed onto its stack. So applying this regex to the input "foo bar" and inspecting

m.Groups["word"].Captures
0: "foo"
1: "bar"

This allows us to even push things onto a single stack from different parts of the expression. But still, this is just .NET's feature of being able to track multiple captures which are listed in this CaptureCollection. But I said, this collection is a stack. So can we pop things from it?

It turns out we can. If we use a group like (?<-word>...), then the last capture is popped from the stack word if the subexpression ... matches. So if we change our previous expression to

(?<word>\w+)\W+(?<-word>\w+)

Then the second group will pop the first group's capture, and we will receive an empty CaptureCollection in the end. Of course, this example is pretty useless.

But there's one more detail to the minus-syntax: if the stack is already empty, the group fails (regardless of its subpattern). We can leverage this behavior to count nesting levels - and this is where the name balancing group comes from (and where it gets interesting). Say we want to match strings that are correctly parenthesized. We push each opening parenthesis on the stack, and pop one capture for each closing parenthesis. If we encounter one closing parenthesis too many, it will try to pop an empty stack and cause the pattern to fail:

So we have three alternatives in a repetition. The first alternative consumes everything that is not a parenthesis. The second alternative matches (s while pushing them onto the stack. The third alternative matches )s while popping elements from the stack (if possible!).

(?=.*[(])

This pattern is not perfect (or entirely correct) though.

There is one more catch: this does not ensure that the stack is empty at the end of the string (hence (foo(bar) would be valid). .NET (and many other flavors) have one more construct that helps us out here: conditional patterns. The general syntax is

(?(condition)truePattern|falsePattern)

where the falsePattern is optional - if it is omitted the false-case will always match. The condition can either be a pattern, or the name of a capturing group. I'll focus on the latter case here. If it's the name of a capturing group, then truePattern is used if and only if the capture stack for that particular group is not empty. That is, a conditional pattern like (?(name)yes|no) reads "if name has matched and captured something (that is still on the stack), use pattern yes otherwise use pattern no".

So at the end of our above pattern we could add something like (?(Open)failPattern) which causes the entire pattern to fail, if the Open-stack is not empty. The simplest thing to make the pattern unconditionally fail is (?!) (an empty negative lookahead). So we have our final pattern:

^(?:[^()]|(?<Open>[(])|(?<-Open>[)]))*(?(Open)(?!))$

Note that this conditional syntax has nothing per se to do with balancing groups but it's necessary to harness their full power.

From here, the sky is the limit. Many very sophisticated uses are possible and there are some gotchas when used in combination with other .NET-Regex features like variable-length lookbehinds (which I had to learn the hard way myself). The main question however is always: is your code still maintainable when using these features? You need to document it really well, and be sure that everyone who works on it is also aware of these features. Otherwise you might be better off, just walking the string manually character-by-character and counting nesting levels in an integer.

Now with all of the above, we can validate that a string is correctly parenthesized. But it would be a lot more useful, if we could actually get (nested) captures for all those parentheses' contents. Of course, we could remember opening and closing parentheses in a separate capture stack that is not emptied, and then do some substring extraction based on their positions in a separate step.

But .NET provides one more convenience feature here: if we use (?<A-B>subPattern), not only is a capture popped from stack B, but also everything between that popped capture of B and this current group is pushed onto stack A. So if we use a group like this for the closing parentheses, while popping nesting levels from our stack, we can also push the pair's content onto another stack:

^(?:[^()]|(?<Open>[(])|(?<Content-Open>[)]))*(?(Open)(?!))$

So taking all of these things together we can:

All in a single regular expression. If that's not exciting... ;)

Some resources that I found helpful when I first learned about them:

c# - What are regular expression Balancing Groups? - Stack Overflow

c# .net regex balancing-groups
Rectangle 27 15

Result is: a ? a : (b ? (c ? c(b) : b) : null)

a ? a : (b ? (c ? c(b) : b) : null)

a
  ? a
  : b
      ? c
        ? c(b)
        : b
      : null

Ternary operator ?: used as inline if-else is right associative. In short this means that the rightmost ? gets fed first and it takes exactly one closest operand on the left and two, with a :, on the right.

Practically speaking, consider the following statement (same as above):

The rightmost ? gets fed first, so find it and its surrounding three arguments and consecutively expand to the left to another ?.

a ? a : b ? c ? c(b) : b : null
                 ^                  <---- RTL
1.            |1-?-2----:-3|
             ^ <-
2.        |1-?|--2---------|:-3---|
     ^ <-
3.|1-?-2-:|--3--------------------|

result: a ? a : (b ? (c ? c(b) : b) : null)
  • Term a is read. Node: a
a ?
  • Term a is read. Node: a ? a
  • Nonterminal ? is read, triggering the right-associativity rule. Associativity decides: node: a ? a : (b ?
  • Term c is read. Node: a ? a : (b ? c
  • Nonterminal ? is read, re-applying the right-associativity rule. Node: a ? a : (b ? (c ?
  • Term c(b) is read. Node: a ? a : (b ? (c ? c(b)
  • Nonterminal : is read. Node: a ? a : (b ? (c ? c(b) :
  • Term b is read. Node: a ? a : (b ? (c ? c(b) : b
  • Nonterminal : is read. The ternary operator ?: from previous scope is satisfied and the scope is closed. Node: a ? a : (b ? (c ? c(b) : b) :
  • Term null is read. Node: a ? a : (b ? (c ? c(b) : b) : null
a
  ? a
  : b
      ? c
        ? c(b)
        : b
      : null
return a + some_lengthy_variable_name > another_variable
        ? "yep"
        : "nop"
var a = 0 // 1
var b = 20
var c = null // x=> {console.log('b is', x); return true} // return true here!

a
  && a
  || b
      && c
        && c(b) // if this returns false, || b is processed
        || b
      || null
function(mayBeNull) {
  var cantBeNull = mayBeNull || 42             // "default" value
  var alsoCantBe = mayBeNull ? mayBeNull : 42  // ugly...
  ..
}
false && (anything) // is short-circuit evaluated to false.
true || (anything)  // is short-circuit evaluated to true.

conditional operator - Javascript one line If...else...else if stateme...

javascript conditional-operator
Rectangle 27 15

Result is: a ? a : (b ? (c ? c(b) : b) : null)

a ? a : (b ? (c ? c(b) : b) : null)

a
  ? a
  : b
      ? c
        ? c(b)
        : b
      : null

Ternary operator ?: used as inline if-else is right associative. In short this means that the rightmost ? gets fed first and it takes exactly one closest operand on the left and two, with a :, on the right.

Practically speaking, consider the following statement (same as above):

The rightmost ? gets fed first, so find it and its surrounding three arguments and consecutively expand to the left to another ?.

a ? a : b ? c ? c(b) : b : null
                 ^                  <---- RTL
1.            |1-?-2----:-3|
             ^ <-
2.        |1-?|--2---------|:-3---|
     ^ <-
3.|1-?-2-:|--3--------------------|

result: a ? a : (b ? (c ? c(b) : b) : null)
  • Term a is read. Node: a
a ?
  • Term a is read. Node: a ? a
  • Nonterminal ? is read, triggering the right-associativity rule. Associativity decides: node: a ? a : (b ?
  • Term c is read. Node: a ? a : (b ? c
  • Nonterminal ? is read, re-applying the right-associativity rule. Node: a ? a : (b ? (c ?
  • Term c(b) is read. Node: a ? a : (b ? (c ? c(b)
  • Nonterminal : is read. Node: a ? a : (b ? (c ? c(b) :
  • Term b is read. Node: a ? a : (b ? (c ? c(b) : b
  • Nonterminal : is read. The ternary operator ?: from previous scope is satisfied and the scope is closed. Node: a ? a : (b ? (c ? c(b) : b) :
  • Term null is read. Node: a ? a : (b ? (c ? c(b) : b) : null
a
  ? a
  : b
      ? c
        ? c(b)
        : b
      : null
return a + some_lengthy_variable_name > another_variable
        ? "yep"
        : "nop"
var a = 0 // 1
var b = 20
var c = null // x=> {console.log('b is', x); return true} // return true here!

a
  && a
  || b
      && c
        && c(b) // if this returns false, || b is processed
        || b
      || null
function(mayBeNull) {
  var cantBeNull = mayBeNull || 42             // "default" value
  var alsoCantBe = mayBeNull ? mayBeNull : 42  // ugly...
  ..
}
false && (anything) // is short-circuit evaluated to false.
true || (anything)  // is short-circuit evaluated to true.

conditional operator - Javascript one line If...else...else if stateme...

javascript conditional-operator
Rectangle 27 35

What's the deal with the (?<A-B>) syntax?

(?<A-B>x) is subtly different from (?<-A>(?<B>x)). They result in the same control flow*, but they capture differently. For example, let's look at a pattern for balanced braces:

(?:[^{}]|(?<B>{)|(?<-B>}))+(?(B)(?!))

At the end of the match we do have a balanced string, but that is all we have - we don't know where the braces are because the B stack is empty. The hard work the engine did for us is gone.(example on Regex Storm)

(?<A-B>x) is the solution for that problem. How? It doesn't capture x into $A: it captures the content between the previous capture of B and the current position.

Let's use it in our pattern:

(?:[^{}]|(?<Open>{)|(?<Content-Open>}))+(?(Open)(?!))
$Content
{1 2 {3} {4 5 {6}} 7}
4 5 {6}
1 2 {3} {4 5 {6}} 7
table
${Content}

In fact, it can be used without balancing at all: (?<A>).(.(?<Content-A>).) captures the first two characters, even though they are separated by groups. (a lookahead is more commonly used here but it doesn't always scale: it may duplicate your logic.)

(?<A-B>) is a strong feature - it gives you exact control over your captures. Keep that in mind when you're trying to get more out of your pattern.

@FYI, continuing the discussion from the question you didn't like in a new answer on this one. :)

I am trying to figure out a way to perform the balanced braces regex check with escaping of braces inside strings. E.G. the following code will pass: public class Foo { private const char BAR = '{'; private string _qux = "{{{"; } Has anyone done this?

@MrAnderson - You just need to add |'[^']*' in the right place: example. If you also need escaped characters, there's an example here: (Regex for matching C# string literals)[stackoverflow.com/a/4953878/7586].

c# - What are regular expression Balancing Groups? - Stack Overflow

c# .net regex balancing-groups
Rectangle 27 34

What's the deal with the (?<A-B>) syntax?

(?<A-B>x) is subtly different from (?<-A>(?<B>x)). They result in the same control flow*, but they capture differently. For example, let's look at a pattern for balanced braces:

(?:[^{}]|(?<B>{)|(?<-B>}))+(?(B)(?!))

At the end of the match we do have a balanced string, but that is all we have - we don't know where the braces are because the B stack is empty. The hard work the engine did for us is gone.(example on Regex Storm)

(?<A-B>x) is the solution for that problem. How? It doesn't capture x into $A: it captures the content between the previous capture of B and the current position.

Let's use it in our pattern:

(?:[^{}]|(?<Open>{)|(?<Content-Open>}))+(?(Open)(?!))
$Content
{1 2 {3} {4 5 {6}} 7}
4 5 {6}
1 2 {3} {4 5 {6}} 7
table
${Content}

In fact, it can be used without balancing at all: (?<A>).(.(?<Content-A>).) captures the first two characters, even though they are separated by groups. (a lookahead is more commonly used here but it doesn't always scale: it may duplicate your logic.)

(?<A-B>) is a strong feature - it gives you exact control over your captures. Keep that in mind when you're trying to get more out of your pattern.

@FYI, continuing the discussion from the question you didn't like in a new answer on this one. :)

I am trying to figure out a way to perform the balanced braces regex check with escaping of braces inside strings. E.G. the following code will pass: public class Foo { private const char BAR = '{'; private string _qux = "{{{"; } Has anyone done this?

@MrAnderson - You just need to add |'[^']*' in the right place: example. If you also need escaped characters, there's an example here: (Regex for matching C# string literals)[stackoverflow.com/a/4953878/7586].

c# - What are regular expression Balancing Groups? - Stack Overflow

c# .net regex balancing-groups
Rectangle 27 12

Wait - How does that work?

With the above code, c is now a read-only property of the Test class.

You can also give a property a setter, which would make it read/write and allow you to set its value directly. It would look like this:

class Test(object):
    def __init__(self, c = SomeDefaultValue):
        self._c = SomeDefaultValue
    @property
    def c(self):
        return self._c
    @c.setter
    def c(self,value):
        self._c = value

However, in this case, it would not make sense to have a setter for self.c, since its value depends on self.a and self.b.

The @property bit is an example of something called a decorator. A decorator actually wraps the function (or class) it decorates into another function (the decorator function). After a function has been decorated, when it is called it is actually the decorator that is called with the function (and its arguments) as an argument. Usually (but not always!) the decorated function does something interesting, and then calls the original (decorated) function like it would normally. For example:

def my_decorator(thedecoratedfunction):
    def wrapped(*allofthearguments):
        print("This function has been decorated!") #something interesting
        thedecoratedfunction(*allofthearguments)   #calls the function as normal
    return wrapped

@my_decorator
def myfunction(arg1, arg2):
    pass
def myfunction(arg1, arg2):
    pass
myfunction = my_decorator(myfunction)

So this means in the class example above, instead of using the decorator you could also do this:

def c(self):
    return self.a + self.b
c = property(c)

They are exactly the same thing. The @property is just syntactic sugar to replace calls for myobject.c with the property getter and setter (deleters are also an option).

You might be wondering why simply doing this once:

myfunction = my_decorator(myfunction)

...results in such a drastic change! So that, from now on, when calling:

myfunction(arg1, arg2)
my_decorator(myfunction)
arg1, arg2
my_decorator
wrapped

All of this works by virtue of something called a closure. When the function is passed into the decorator in this way (e.g., property(c)), the function's name is re-bound to the wrapped version of the function instead of the original function, and the original function's arguments are always passed to wrapped instead of the original function. This is simply the way that closures work, and there's nothing magical about it. Here is some more information about closures.

So to summarize so far: @property is just a way of wrapping the class method inside of the property() function so the wrapped class method is called instead of the original, unwrapped class method. But what is the property function? What does it do?

The property function adds something called a descriptor to the class. Put simply, a descriptor is an object class that can have separate get, set, and delete methods. When you do this:

@property
def c(self):
    return self._c

...you are adding a descriptor to the Test class called c, and defining the get method (actually, __get__()) of the c descriptor as equal to the c(self) method.

@c.setter
def c(self,value):
    self._c

...you are defining the set method (actually, __set__()) of the c descriptor as equal to the c(self,value) method.

An amazing amount of stuff is accomplished by simply adding @property to your def c(self) method! In practice, you probably don't need to understand all of this right away to begin using it. However, I recommend keeping in mind that when you use @property, you are using decorators, closures, and descriptors, and if you are at all serious about learning Python it would be well worth your time to investigate each of these topics on their own.

Thanks, your first draft was good enough for me to head in the right direction, now it's too much info! :-)

@name_masked i think there is never too much info ... For a beginner to decorators, this is a good entry to getters and setters

python - Dynamically update attributes of an object that depend on the...

python
Rectangle 27 65

An overview of operators in PHP:

  • $a && $b : TRUE if both $a and $b are TRUE.
  • $a || $b : TRUE if either $a or $b is TRUE.
  • $a xor $b : TRUE if either $a or $b is TRUE, but not both.
  • ! $a : TRUE if $a is not TRUE.
  • $a and $b : TRUE if both $a and $b are TRUE.
  • $a or $b : TRUE if either $a or $b is TRUE.
  • $a == $b : TRUE if $a is equal to $b after type juggling.
  • $a === $b : TRUE if $a is equal to $b, and they are of the same type.
  • $a !== $b : TRUE if $a is not equal to $b, or they are not of the same type.
  • $a < $b : TRUE if $a is strictly less than $b.
  • $a > $b : TRUE if $a is strictly greater than $b.
  • $a <= $b : TRUE if $a is less than or equal to $b.
  • $a >= $b : TRUE if $a is greater than or equal to $b.
  • $a <=> $b : An integer less than, equal to, or greater than zero when $a is respectively less than, equal to, or greater than $b. Available as of PHP 7.
  • -$a : Opposite of $a.
  • $a + $b : Sum of $a and $b.
  • $a - $b : Difference of $a and $b.
  • $a * $b : Product of $a and $b.
  • $a / $b : Quotient of $a and $b.
  • $a % $b : Remainder of $a divided by $b.
  • $a ** $b : Result of raising $a to the $b'th power (introduced in PHP 5.6)
  • $a & $b : Bits that are set in both $a and $b are set.
  • $a | $b : Bits that are set in either $a or $b are set.
  • $a ^ $b : Bits that are set in $a or $b but not both are set.
  • ~ $a : Bits that are set in $a are not set, and vice versa.
  • $a << $b : Shift the bits of $a $b steps to the left (each step means "multiply by two")
  • $a >> $b : Shift the bits of $a $b steps to the right (each step means "divide by two")
  • $a . $b : Concatenation of $a and $b.
  • $a + $b : Union of $a and $b.
  • $a == $b : TRUE if $a and $b have the same key/value pairs.
  • $a === $b : TRUE if $a and $b have the same key/value pairs in the same order and of the same types.
  • $a != $b : TRUE if $a is not equal to $b.
  • $a <> $b : TRUE if $a is not equal to $b.
  • $a !== $b : TRUE if $a is not identical to $b.
  • $a = $b : The value of $b is assigned to $a
  • $a += $b : Same as $a = $a + $b
  • $a -= $b : Same as $a = $a - $b
  • $a *= $b : Same as $a = $a * $b
  • $a /= $b : Same as $a = $a / $b
  • $a %= $b : Same as $a = $a % $b
  • $a **= $b : Same as $a = $a ** $b
  • $a .= $b : Same as $a = $a . $b
  • $a &= $b : Same as $a = $a & $b
  • $a |= $b : Same as $a = $a | $b
  • $a ^= $b : Same as $a = $a ^ $b
  • $a <<= $b : Same as $a = $a << $b
  • $a >>= $b : Same as $a = $a >> $b

and operator and or operator have lower precedence than assignment operator =.

$a = true and false;
($a = true) and false

In most cases you will probably want to use && and ||, which behave in a way known from languages like C, Java or JavaScript.

operators - Reference — What does this symbol mean in PHP? - Stack Ove...

php operators symbols
Rectangle 27 64

An overview of operators in PHP:

  • $a && $b : TRUE if both $a and $b are TRUE.
  • $a || $b : TRUE if either $a or $b is TRUE.
  • $a xor $b : TRUE if either $a or $b is TRUE, but not both.
  • ! $a : TRUE if $a is not TRUE.
  • $a and $b : TRUE if both $a and $b are TRUE.
  • $a or $b : TRUE if either $a or $b is TRUE.
  • $a == $b : TRUE if $a is equal to $b after type juggling.
  • $a === $b : TRUE if $a is equal to $b, and they are of the same type.
  • $a !== $b : TRUE if $a is not equal to $b, or they are not of the same type.
  • $a < $b : TRUE if $a is strictly less than $b.
  • $a > $b : TRUE if $a is strictly greater than $b.
  • $a <= $b : TRUE if $a is less than or equal to $b.
  • $a >= $b : TRUE if $a is greater than or equal to $b.
  • $a <=> $b : An integer less than, equal to, or greater than zero when $a is respectively less than, equal to, or greater than $b. Available as of PHP 7.
  • -$a : Opposite of $a.
  • $a + $b : Sum of $a and $b.
  • $a - $b : Difference of $a and $b.
  • $a * $b : Product of $a and $b.
  • $a / $b : Quotient of $a and $b.
  • $a % $b : Remainder of $a divided by $b.
  • $a ** $b : Result of raising $a to the $b'th power (introduced in PHP 5.6)
  • $a & $b : Bits that are set in both $a and $b are set.
  • $a | $b : Bits that are set in either $a or $b are set.
  • $a ^ $b : Bits that are set in $a or $b but not both are set.
  • ~ $a : Bits that are set in $a are not set, and vice versa.
  • $a << $b : Shift the bits of $a $b steps to the left (each step means "multiply by two")
  • $a >> $b : Shift the bits of $a $b steps to the right (each step means "divide by two")
  • $a . $b : Concatenation of $a and $b.
  • $a + $b : Union of $a and $b.
  • $a == $b : TRUE if $a and $b have the same key/value pairs.
  • $a === $b : TRUE if $a and $b have the same key/value pairs in the same order and of the same types.
  • $a != $b : TRUE if $a is not equal to $b.
  • $a <> $b : TRUE if $a is not equal to $b.
  • $a !== $b : TRUE if $a is not identical to $b.
  • $a = $b : The value of $b is assigned to $a
  • $a += $b : Same as $a = $a + $b
  • $a -= $b : Same as $a = $a - $b
  • $a *= $b : Same as $a = $a * $b
  • $a /= $b : Same as $a = $a / $b
  • $a %= $b : Same as $a = $a % $b
  • $a **= $b : Same as $a = $a ** $b
  • $a .= $b : Same as $a = $a . $b
  • $a &= $b : Same as $a = $a & $b
  • $a |= $b : Same as $a = $a | $b
  • $a ^= $b : Same as $a = $a ^ $b
  • $a <<= $b : Same as $a = $a << $b
  • $a >>= $b : Same as $a = $a >> $b

and operator and or operator have lower precedence than assignment operator =.

$a = true and false;
($a = true) and false

In most cases you will probably want to use && and ||, which behave in a way known from languages like C, Java or JavaScript.

operators - Reference — What does this symbol mean in PHP? - Stack Ove...

php operators symbols
Rectangle 27 64

An overview of operators in PHP:

  • $a && $b : TRUE if both $a and $b are TRUE.
  • $a || $b : TRUE if either $a or $b is TRUE.
  • $a xor $b : TRUE if either $a or $b is TRUE, but not both.
  • ! $a : TRUE if $a is not TRUE.
  • $a and $b : TRUE if both $a and $b are TRUE.
  • $a or $b : TRUE if either $a or $b is TRUE.
  • $a == $b : TRUE if $a is equal to $b after type juggling.
  • $a === $b : TRUE if $a is equal to $b, and they are of the same type.
  • $a !== $b : TRUE if $a is not equal to $b, or they are not of the same type.
  • $a < $b : TRUE if $a is strictly less than $b.
  • $a > $b : TRUE if $a is strictly greater than $b.
  • $a <= $b : TRUE if $a is less than or equal to $b.
  • $a >= $b : TRUE if $a is greater than or equal to $b.
  • $a <=> $b : An integer less than, equal to, or greater than zero when $a is respectively less than, equal to, or greater than $b. Available as of PHP 7.
  • -$a : Opposite of $a.
  • $a + $b : Sum of $a and $b.
  • $a - $b : Difference of $a and $b.
  • $a * $b : Product of $a and $b.
  • $a / $b : Quotient of $a and $b.
  • $a % $b : Remainder of $a divided by $b.
  • $a ** $b : Result of raising $a to the $b'th power (introduced in PHP 5.6)
  • $a & $b : Bits that are set in both $a and $b are set.
  • $a | $b : Bits that are set in either $a or $b are set.
  • $a ^ $b : Bits that are set in $a or $b but not both are set.
  • ~ $a : Bits that are set in $a are not set, and vice versa.
  • $a << $b : Shift the bits of $a $b steps to the left (each step means "multiply by two")
  • $a >> $b : Shift the bits of $a $b steps to the right (each step means "divide by two")
  • $a . $b : Concatenation of $a and $b.
  • $a + $b : Union of $a and $b.
  • $a == $b : TRUE if $a and $b have the same key/value pairs.
  • $a === $b : TRUE if $a and $b have the same key/value pairs in the same order and of the same types.
  • $a != $b : TRUE if $a is not equal to $b.
  • $a <> $b : TRUE if $a is not equal to $b.
  • $a !== $b : TRUE if $a is not identical to $b.
  • $a = $b : The value of $b is assigned to $a
  • $a += $b : Same as $a = $a + $b
  • $a -= $b : Same as $a = $a - $b
  • $a *= $b : Same as $a = $a * $b
  • $a /= $b : Same as $a = $a / $b
  • $a %= $b : Same as $a = $a % $b
  • $a **= $b : Same as $a = $a ** $b
  • $a .= $b : Same as $a = $a . $b
  • $a &= $b : Same as $a = $a & $b
  • $a |= $b : Same as $a = $a | $b
  • $a ^= $b : Same as $a = $a ^ $b
  • $a <<= $b : Same as $a = $a << $b
  • $a >>= $b : Same as $a = $a >> $b

and operator and or operator have lower precedence than assignment operator =.

$a = true and false;
($a = true) and false

In most cases you will probably want to use && and ||, which behave in a way known from languages like C, Java or JavaScript.

operators - Reference — What does this symbol mean in PHP? - Stack Ove...

php operators symbols
Rectangle 27 307

Python 3.x makes a clear distinction between the types:

  • str = '...' literals = a sequence of Unicode characters (UTF-16 or UTF-32, depending on how Python was compiled)
  • bytes = b'...' literals = a sequence of octets (integers between 0 and 255)

If you're familiar with Java or C#, think of str as String and bytes as byte[]. If you're familiar with SQL, think of str as NVARCHAR and bytes as BINARY or BLOB. If you're familiar with the Windows registry, think of str as REG_SZ and bytes as REG_BINARY. If you're familiar with C(++), then forget everything you've learned about char and strings, because A CHARACTER IS NOT A BYTE. That idea is long obsolete.

You use str when you want to represent text.

print(' ')
bytes
NaN = struct.unpack('>d', b'\xff\xf8\x00\x00\x00\x00\x00\x00')[0]

You can encode a str to a bytes object.

>>> '\uFEFF'.encode('UTF-8')
b'\xef\xbb\xbf'

And you can decode a bytes into a str.

>>> b'\xE2\x82\xAC'.decode('UTF-8')
''

But you can't freely mix the two types.

>>> b'\xEF\xBB\xBF' + 'Text with a UTF-8 BOM'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't concat bytes to str

The b'...' notation is somewhat confusing in that it allows the bytes 0x01-0x7F to be specified with ASCII characters instead of hex numbers.

>>> b'A' == b'\x41'
True

a character is not a byte

>>> 'A' == b'A'
False

Pre-3.0 versions of Python lacked this kind of distinction between text and binary data. Instead, there was:

  • unicode = u'...' literals = sequence of Unicode characters = 3.x str
str
'...'
struct.pack

In order to ease the 2.x-to-3.x transition, the b'...' literal syntax was backported to Python 2.6, in order to allow distinguishing binary strings (which should be bytes in 3.x) from text strings (which should be str in 3.x). The b prefix does nothing in 2.x, but tells the 2to3 script not to convert it to a Unicode string in 3.x.

So yes, b'...' literals in Python have the same purpose that they do in PHP.

Also, just out of curiosity, are there more symbols than the b and u that do other things?

The r prefix creates a raw string (e.g., r'\t' is a backslash + t instead of a tab), and triple quotes '''...''' or """...""" allow multi-line string literals.

Thanks! I understood it after reading these sentences: "In order to ease the 2.x-to-3.x transition, the b'...' literal syntax was backported to Python 2.6, in order to allow distinguishing binary strings (which should be bytes in 3.x) from text strings (which should be str in 3.x). The b prefix does nothing in 2.x, but tells the 2to3 script not to convert it to a Unicode string in 3.x."

Very comprehensive, sadly it doesn't explain how to get a meaningful Py3 str() from a bytes() object

The 'A' == b'A' --> False check really makes it clear. The rest of it is excellent, but up to that point I hadn't properly understood that a byte string is not really text.

python - What does the 'b' character do in front of a string literal? ...

python string unicode binary
Rectangle 27 307

Python 3.x makes a clear distinction between the types:

  • str = '...' literals = a sequence of Unicode characters (UTF-16 or UTF-32, depending on how Python was compiled)
  • bytes = b'...' literals = a sequence of octets (integers between 0 and 255)

If you're familiar with Java or C#, think of str as String and bytes as byte[]. If you're familiar with SQL, think of str as NVARCHAR and bytes as BINARY or BLOB. If you're familiar with the Windows registry, think of str as REG_SZ and bytes as REG_BINARY. If you're familiar with C(++), then forget everything you've learned about char and strings, because A CHARACTER IS NOT A BYTE. That idea is long obsolete.

You use str when you want to represent text.

print(' ')
bytes
NaN = struct.unpack('>d', b'\xff\xf8\x00\x00\x00\x00\x00\x00')[0]

You can encode a str to a bytes object.

>>> '\uFEFF'.encode('UTF-8')
b'\xef\xbb\xbf'

And you can decode a bytes into a str.

>>> b'\xE2\x82\xAC'.decode('UTF-8')
''

But you can't freely mix the two types.

>>> b'\xEF\xBB\xBF' + 'Text with a UTF-8 BOM'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't concat bytes to str

The b'...' notation is somewhat confusing in that it allows the bytes 0x01-0x7F to be specified with ASCII characters instead of hex numbers.

>>> b'A' == b'\x41'
True

a character is not a byte

>>> 'A' == b'A'
False

Pre-3.0 versions of Python lacked this kind of distinction between text and binary data. Instead, there was:

  • unicode = u'...' literals = sequence of Unicode characters = 3.x str
str
'...'
struct.pack

In order to ease the 2.x-to-3.x transition, the b'...' literal syntax was backported to Python 2.6, in order to allow distinguishing binary strings (which should be bytes in 3.x) from text strings (which should be str in 3.x). The b prefix does nothing in 2.x, but tells the 2to3 script not to convert it to a Unicode string in 3.x.

So yes, b'...' literals in Python have the same purpose that they do in PHP.

Also, just out of curiosity, are there more symbols than the b and u that do other things?

The r prefix creates a raw string (e.g., r'\t' is a backslash + t instead of a tab), and triple quotes '''...''' or """...""" allow multi-line string literals.

Thanks! I understood it after reading these sentences: "In order to ease the 2.x-to-3.x transition, the b'...' literal syntax was backported to Python 2.6, in order to allow distinguishing binary strings (which should be bytes in 3.x) from text strings (which should be str in 3.x). The b prefix does nothing in 2.x, but tells the 2to3 script not to convert it to a Unicode string in 3.x."

Very comprehensive, sadly it doesn't explain how to get a meaningful Py3 str() from a bytes() object

The 'A' == b'A' --> False check really makes it clear. The rest of it is excellent, but up to that point I hadn't properly understood that a byte string is not really text.

python - What does the 'b' character do in front of a string literal? ...

python string unicode binary
Rectangle 27 346

Yes, you can do that, but only if #b is after #a in the HTML.

#a:hover + #b {
    background: #ccc
}

<div id="a">Div A</div>
<div id="b">Div B</div>

If there are other elements between #a and #b, you can use this: http://jsfiddle.net/u7tYE/1/

#a:hover ~ #b {
    background: #ccc
}

<div id="a">Div A</div>
<div>random other elements</div>
<div>random other elements</div>
<div>random other elements</div>
<div id="b">Div B</div>

Both + and ~ work in all modern browsers and IE7+

If #b is a descendant of #a, you can simply use #a:hover #b.

ALTERNATIVE: You can use pure CSS to do this by positioning the second element before the first. The first div is first in markup, but positioned to the right or below the second. It will work as if it were a previous sibling.

it should be noted that the sibling selector (+) doesn't work correctly in ie8 and below.

<div id = "asdf"><div id = "affect_this_element?">...</div></div>
#asdf:hover > #affect_this_element

Cool, the child selector > works, thanks @thirtydot!

#a
#b
<div class="wrapper01"><div id="a">Div A</div></div>
<div class="wrapper02"><div id="b">Div B</div></div>

On a CSS hover event, can I change another div's styling? - Stack Over...

css
Rectangle 27 341

Yes, you can do that, but only if #b is after #a in the HTML.

#a:hover + #b {
    background: #ccc
}

<div id="a">Div A</div>
<div id="b">Div B</div>

If there are other elements between #a and #b, you can use this: http://jsfiddle.net/u7tYE/1/

#a:hover ~ #b {
    background: #ccc
}

<div id="a">Div A</div>
<div>random other elements</div>
<div>random other elements</div>
<div>random other elements</div>
<div id="b">Div B</div>

Both + and ~ work in all modern browsers and IE7+

If #b is a descendant of #a, you can simply use #a:hover #b.

ALTERNATIVE: You can use pure CSS to do this by positioning the second element before the first. The first div is first in markup, but positioned to the right or below the second. It will work as if it were a previous sibling.

it should be noted that the sibling selector (+) doesn't work correctly in ie8 and below.

<div id = "asdf"><div id = "affect_this_element?">...</div></div>
#asdf:hover > #affect_this_element

Cool, the child selector > works, thanks @thirtydot!

#a
#b
<div class="wrapper01"><div id="a">Div A</div></div>
<div class="wrapper02"><div id="b">Div B</div></div>

On a CSS hover event, can I change another div's styling? - Stack Over...

css
Rectangle 27 12

if not a or (a and b):
    print('foo')
print('foo')
  • When a and b are both True.
  • When else is executed, what is else? The opposite of the previous if so not a.

Finally you wish to display 'foo' in one case or the other.

EDIT: Alternatively, by simplifying the logic equation:

if not a or b: 
   print('foo')

Because if not a is not True, then a must be True (the second part of or), so the a and b can be simplified in just b (because we do know as a fact that a is True in this situation, so a and b is the same as True and b so we can drop the first part safely).

a(ab)ab

Thanks! The logic is pretty simple so I probably wont have any problems, but I will be careful

if statement - Python: Check second variable if first variable is True...

python if-statement python-3.x
Rectangle 27 5

(not b and a) or (not a and b)

will give a if b is false will give b if a is false will give False otherwise

Or with the Python 2.5+ ternary expression:

(False if a else b) if b else a

logic - How do you get the logical xor of two variables in Python? - S...

python logic
Rectangle 27 134

IF you have tables A and B, both with colum C, here are the records, which are present in table A but not in B:

SELECT A.*
FROM A
    LEFT JOIN B ON (A.C = B.C)
WHERE B.C IS NULL

To get all the differences with a single query, a full join must be used, like this:

SELECT A.*, B.*
FROM A
    FULL JOIN B ON (A.C = B.C)
WHERE A.C IS NULL OR B.C IS NULL

What you need to know in this case is, that when a record can be found in A, but not in B, than the columns which come from B will be NULL, and similarly for those, which are present in B and not in A, the columns from A will be null.

The problem might be that you cannot compare a value with null using '='. (Or at least when SET ANSI_NULLS is ON.) You must say: value IS NULL or value IS NOT NULL.

I'm marking this as the answer I used because in doing this way I was able to easily do a few other things I had to later on.

The objects "a.dbo.student" and "b.dbo.student" in the FROM clause have the same exposed names. Use correlation names to distinguish them.

@Thecrocodilehunter you need to change name of tables like a.dbo.student as and b.dbo.student bs then refer to tables with as and bs

sql server - sql query to return differences between two tables - Stac...

sql sql-server sql-server-2008 sql-server-2005
Rectangle 27 27

Your a b c: rule tells Make that this is how to build any of these targets, not how to build all of them. Make is not smart enough to analyze the commands and deduce that running the rule once will build all three. Make knows (from the output rule) that it must rebuild a, b and c, so that's what it does. It runs the first rule once for a, once for b and once for c.

If you want to rebuild them individually, do this:

a b c:
    echo "Creating $@"
    touch $@

If you want to rebuild them all at once, do something like this:

.PHONY: things
things:
    echo "Creating a b c"
    touch a b c

output: things
    cat a b c > output
THINGS = a b c
.PHONY: things
things:
    echo "Creating $(THINGS)"
    touch $(THINGS)

output: things
    cat $(THINGS) > output

Great answer! Thank you. Is creating a phony target the only way, or is there a cleaner way to express "a and b and c are built by this rule"?

@Pavel, I don't know of a cleaner way (and I would probably take the first option anyhow). But then again I'm still trying to figure out AUZKamath's answer.

See cmcrossroads.com/ask-mr-make/ for a survey of methods to deal with this scenario, including the only "correct" way to specify a rule with multiple outputs in gmake (pattern rules, of course!).

makefile - GNU make: Multiple targets in a single rule - Stack Overflo...

makefile gnu-make
Rectangle 27 171

I noticed that the address stored in ptr was always being overwritten with 00008123...

This seemed odd, so I did a little digging and found this Microsoft blog post containing a section discussing "Automated pointer sanitization when deleting C++ objects".

...checks for NULL are a common code construct meaning that an existing check for NULL combined with using NULL as a sanitization value could fortuitously hide a genuine memory safety issue whose root cause really does needs addressing.

For this reason we have chosen 0x8123 as a sanitization value from an operating system perspective this is in the same memory page as the zero address (NULL), but an access violation at 0x8123 will better stand out to the developer as needing more detailed attention.

Not only does it explain what Visual Studio does with the pointer after it is deleted, it also answers why they chose NOT to set it to NULL automatically!

Changing this setting and rerunning the same code produces the following output:

ptr = 007CBC10
ptr = 007CBC10

"feature" is in quotes because in a case where you have two pointers to the same location, calling delete will only sanitize ONE of them. The other one will be left pointing to the invalid location.

That's a nice find. I wish MS would better document debugging behavior like this. For example, it would be nice to know which compiler version started implementing this and what options enable/disable the behavior.

"from an operating system perspective this is in the same memory page as the zero address" - huh? Isn't the standard (ignoring large pages) page size on x86 still 4kb for both windows and linux? Although I dimly remember something about the first 64kb of address space on Raymond Chen's blog, so in practice I take it same result,

@Voo windows reserves the first (and last) 64kB worth of RAM as dead space for trapping. 0x8123 falls in there nicely

Actually, it doesn't encourage bad habits, and it doesn't allow you to skip setting the pointer to NULL - that's the whole reason they're using 0x8123 instead of 0. The pointer is still invalid, but causes an exception when attempting to dereference it (good), and it doesn't pass NULL checks (also good, because it's an error not to do that). Where's the place for bad habits? It really is just something that helps you debug.

Well, it can't set both (all) of them, so this is the second best option. If you don't like it, just turn off the SDL checks - I find them rather useful, especially when debugging someone else's code.

c++ - What does Visual Studio do with a deleted pointer and why? - Sta...

c++ pointers visual-studio-2012 memory-management delete-operator
Rectangle 27 8

The problem with Person(String name) and Person(String address) is that you can't have two constructors with the same parameters. The compiler will not know which one to call when you want to call something like this: new Person("Joe Blow");

Person(String name)
{
    this.name = name;
}
Person(String name, String address)
{
    this(name);
    this.address = address;
}

The "this()" in your last constructor is just telling that constructor to call the default constructor as part of the process of constructing the object. It does not create two objects, it just runs the code in the def. constructor, which in your case, does nothing.

Good answer. However, why not go the reverse way with this(String, "") (see my answer)?

Yeah, that's a good point +1 for your answer. Have the more general constructors call the more specific ones.

java - What does "this()" do in a constructor? - Stack Overflow

java constructor