FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Requesting Help with a Regular Expression
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
Requesting Help with a Regular Expression [message #179442] Sun, 28 October 2012 23:08 Go to next message
bobgatski is currently offline  bobgatski
Messages: 11
Registered: October 2012
Karma: 0
Junior Member
From strings such as these ...
int(11)
varchar(45)
datetime
.... I am trying to extract just the type and length; i.e. for the type I want ...
int
varchar
datetime
.... and for the length I want ...
11
45
NULL
I can get the type using ...
(\w+)
.... but I haven't been able to get the type and length. What I think should work is ...
(\w+)(\((\d+)\))?
.... gets no matches at all,not even the type.

I knew that I wasn't a regular expression expert but I thought I could handle something this easy.

Thanks for any help, advice, sympathy anyone can offer.

Bob
Re: Requesting Help with a Regular Expression [message #179443 is a reply to message #179442] Sun, 28 October 2012 23:39 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Sun, 28 Oct 2012 16:08:20 -0700, bobgatski wrote:

> From strings such as these ...
> int(11)
> varchar(45)
> datetime ... I am trying to extract just the type and length; i.e. for
> the type I want ...
> int varchar datetime ... and for the length I want ...
> 11 45 NULL I can get the type using ...
> (\w+)
> ... but I haven't been able to get the type and length. What I think
> should work is ... (\w+)(\((\d+)\))?
> ... gets no matches at all,not even the type.

Try:

preg_match( "/(\w+)\(?(\d+)?/", $target, $arr );

<?php

$arr = array();
$arr[] = "int(11)";
$arr[] = "varchar(45)";
$arr[] = "datetime";

foreach ( $arr as $x ) {
if ( preg_match( "/(\w+)\(?(\d+)?/", $x, $arr2 ) == 1 ) {
echo "\$x = \"{$x}\"";
echo ", \$arr2[0] = \"{$arr2[0]}\"";
echo ", \$arr2[1] = \"{$arr2[1]}\"";
if ( isset( $arr2[2] ) )
echo ", \$arr2[2] = \"{$arr2[2]}\"";
echo "\n";
}
else {
echo "\$x = \"{$x}\" - No match or regex error!\n";
}
}
Re: Requesting Help with a Regular Expression [message #179453 is a reply to message #179443] Mon, 29 October 2012 14:34 Go to previous message
bobgatski is currently offline  bobgatski
Messages: 11
Registered: October 2012
Karma: 0
Junior Member
On Sunday, October 28, 2012 7:39:39 PM UTC-4, Denis McMahon wrote:
> On Sun, 28 Oct 2012 16:08:20 -0700, bobgatski wrote:
>
>
>
>> From strings such as these ...
>
>> int(11)
>
>> varchar(45)
>
>> datetime ... I am trying to extract just the type and length; i.e. for
>
>> the type I want ...
>
>> int varchar datetime ... and for the length I want ...
>
>> 11 45 NULL I can get the type using ...
>
>> (\w+)
>
>> ... but I haven't been able to get the type and length. What I think
>
>> should work is ... (\w+)(\((\d+)\))?
>
>> ... gets no matches at all,not even the type.
>
>
>
> Try:
>
>
>
> preg_match( "/(\w+)\(?(\d+)?/", $target, $arr );
>
>
>
> <?php
>
>
>
> $arr = array();
>
> $arr[] = "int(11)";
>
> $arr[] = "varchar(45)";
>
> $arr[] = "datetime";
>
>
>
> foreach ( $arr as $x ) {
>
> if ( preg_match( "/(\w+)\(?(\d+)?/", $x, $arr2 ) == 1 ) {
>
> echo "\$x = \"{$x}\"";
>
> echo ", \$arr2[0] = \"{$arr2[0]}\"";
>
> echo ", \$arr2[1] = \"{$arr2[1]}\"";
>
> if ( isset( $arr2[2] ) )
>
> echo ", \$arr2[2] = \"{$arr2[2]}\"";
>
> echo "\n";
>
> }
>
> else {
>
> echo "\$x = \"{$x}\" - No match or regex error!\n";
>
> }
>
> }

Denis, Thank you very, VERY much. I'm very new to PHP so I don't fully understand all of your code, but I've tried it and it works and I am learning the language elements I need to fully understand it. Thanks again, Bob
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: How to cURL a JSP page
Next Topic: Beginner's trouble with substr
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Thu Dec 19 15:12:21 GMT 2024

Total time taken to generate the page: 0.02202 seconds