Regex - help me get the grips with it...

Soldato
Joined
12 Jun 2005
Posts
5,361
Hi there,

Still don't know how to use this probably, I have a feeling i may have posted topics on this before, but want to get it under my belt once and for all! :p

....anywho, I was wondering if anyone had a good online tutorials that i could read, or even better Video tutorials (i can learn easier from those).

....also, I was wondering if there is any good freeware so I can practise on....So I have had a string and have an input box for a regualar expression and run it and it'll give me the result. Possibly a plugin for notepad++?

Thanks.
 
Right i am having problems with finding someone inbetween something else but not including it in the result.

For instance if i have:

Code:
...<b>hey stuff 10sdd f</b>...

What I want is:

Code:
hey stuff 10sdd f

What i am using is this:

Code:
<b>(.*)</b>

...and I think that the stuff between the brackets it captured to a parameter without the <b> tags.....but i am unsure of how to get that parameter. I am using C#.NET

Thanks.
 
Dunno what .NET's syntax is, but lookup back references. In PHP that would be:
Code:
preg_replace('<b>(.*)</b>', '$1', $aString);

(there are better ways for exactly this, but it's a demonstration)
 
Found, what i am looking for.

In .NET it seems when i run that code it gives me two results....one with the <b> tag and one without.

Thanks.
 
Dj_jester

Does it do the same in PHP, what would the result be for this:

Code:
preg_replace('<b>(.*)</b>', '[b]$0[/b]', $aString);

....would it replace the whole string?
 
It would do nothing, the 0th backreference is the whole matching string. The first backreference is the first group (the bit in brackets).

so

PHP:
$foo = 'some random string with <b>a bold bit</b>';
$foo = preg_replace('/<b>(.*)<\/b>/', '$0', $foo);
echo $foo; // outputs: some random string with <b>a bold bit</b>


$foo = 'some random string with <b>a bold bit</b>';
$foo = preg_replace('/<b>(.*)<\/b>/', '$1', $foo);
echo $foo; // outputs: some random string with a bold bit

$foo = 'some random string with <b>a bold bit</b>';
$matches = array();
if (preg_match('/<b>(.*)<\/b>/', $foo, $matches)) {
    print_r($matches);
}
/* This outputs:
 * Array
 * {
 *     [0] => <b>a bold bit</b>
 *     [1] => a bold bit
 * }
 */

Hope that helps

Just a quick note, I'm not sure if it's the same in .NET's regexp object. But .* is a "greedy" match in PCRE. This means that the pattern '/<b>(.*)<\/b>/' run against the string 'some string <b>with</b> lots <b>of</b> <b>bold</b> bits.' would have 'with</b> lots <b>of</b> <b>bold' for the first group, IE it matches as much as it can. The ungreedy pattern is '/<b>(.*?)<\/b>/' and would match just 'with', IE as little as possible.
 
Last edited:
Use the regex class:

const string regexString = @"<b>(?<someParamName>.*?)</b>";
Regex propertyGet = new Regex(regexString , RegexOptions.ExplicitCapture);
Match bob = propertyGet.Match("...<b>hey stuff 10sdd f</b>...");
string result = bob.Groups["someParamName"].Value.Trim();

Best thing about .net is the fact you can used name reference groups :)
 
Back
Top Bottom