Hello,
I want to count underscores in a string. I already found a solution:
This works fine, but I would like to count only underscores which are not preceded by a backslash. So, for example, in the string "foo_bar_bar\_foo" $underscore_count would be 2.
I tried this:
But the results are very strange. Underscores without backslash are counted correct, but if I have two '_' and one '\_' in the string, he counts 4.
So my question actually is: How can I match a string which is not preceded by another string?
Thanks in advance for any clues and sorry for my bad english.
I want to count underscores in a string. I already found a solution:
Code:
$underscore_count = ($string =~ tr/_//);
This works fine, but I would like to count only underscores which are not preceded by a backslash. So, for example, in the string "foo_bar_bar\_foo" $underscore_count would be 2.
I tried this:
Code:
$underscore_count = ($string =~ tr/[^\\]_//);
But the results are very strange. Underscores without backslash are counted correct, but if I have two '_' and one '\_' in the string, he counts 4.
So my question actually is: How can I match a string which is not preceded by another string?
Thanks in advance for any clues and sorry for my bad english.