6

I have problem with regex. I've been playing with it for three hours and I didn't find out anything working.

I have this text:

Fax received from 45444849 ( 61282370000 )

And I need to extract the number from brackets, so I will get 61282370000. If there is nothing (or whitespaces only) in brackets it should take the number before brackets. I have only managed to do this expression, which takes the number from brackets correctly:

Fax received from .* \(\s([^)]*)\s\)$

Thanks.

3
  • 1
    I would advise that rather than trying to construct a regex which takes either that you construct a regex which takes the one in the brackets - if this fails then another regex takes the one before...
    – El Ronnoco
    May 10, 2011 at 9:22
  • The problem is, that I have to setup application, which can take only one result group (either the number from brackets, or the number before, if there is nothing in brackets). If I have the possibility to edit it on application level, I would do it rather, than finding this solution, but I can't. I've tried the alternate symbol "|", but I haven't done anything useful. May 10, 2011 at 9:27
  • Try using "||". For eg./\(\s*(\d+)/||/(\d+)/ achieves the same thing. The matched no. will always be present in $1
    – kshenoy
    May 10, 2011 at 12:04

5 Answers 5

10

Try the regex /(\d+)(?!\D*\d+)/ It uses negative lookahead to capture the last number in the string.

For eg.

perl -le '$_="Fax received from 45444849 ( 61282370000 )"; /(\d+)(?!\D*\d+)/; print $1'

will give you 61282370000. However,

perl -le '$_="Fax received from 45444849 (  )"; /(\d+)(?!\D*\d+)/; print $1'

gives 45444849 in $1

0
1

Pseudocode...

if str.match("\(\s*(\d+)\s*\)") 
   return str.matches("\(\s*(\d+)\s*\)")[0]
else
   return str.matches("(\d+)")[0]
0
1

You should try to match on both... then use if... assume the data is in $line...

$line =~ /Fax\sreceived.+?(\d+)\s+\(\s*(\S+)?\s+\)/;
if ($2) {$result= $2;} else {$result= $1;}

Examples...

$line1 = "Fax received from 45444849 ( 61282370000 )";
$line1 =~ /Fax\sreceived.+?(\d+)\s+\(\s*(\S+)?\s+\)/;
if ($2) {$result= $2;} else {$result= $1;}
print "result1: $result\n";

$line2 = "Fax received from 95551212 ( )";
$line2 =~ /Fax\sreceived.+?(\d+)\s+\(\s*(\S+)?\s+\)/;
if ($2) {$result= $2;} else {$result= $1;}
print "result2: $result\n";

Running that yields...

[mpenning@Bucksnort ~]$ perl fax.pl
result1: 61282370000
result2: 95551212
[mpenning@Bucksnort ~]$
1

In Oracle PL/SQL, I should write as follows:

SELECT TRIM (
          REPLACE (
             REPLACE (
                REGEXP_REPLACE (
                   'Fax received from 323 ( 123 )',
                   '[ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789]*( [0123456789]* )',
                   '',
                   1,
                   1,
                   'cm'),
                ')',
                ''),
             '(',
             ''))
  FROM DUAL;

The result of the SELECTed expression is 123.

1
  • 1
    I'm still trying to figure out why you're posting a SQL query as a response to this question May 10, 2011 at 11:58
1

If this is perl, you don't need to do the selection logic in the regex. You simply need to capture both and select, like so:

my $number = List::Util::first { $_; } m/(\d{7,})\s*[(]\s*(\d{7,})?\s*[)]/;
# deal with $number...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.