-1

I have a document with IEEE style citations, or numbers in brackets. They can be one number, as in [23], or several, as in [5, 7, 14], or a range, as in [12-15].

What I have now is [\[|\s|-]([0-9]{1,3})[\]|,|-].

This is capturing single numbers, and the first number in a group, but not subsequent numbers or either number in a range. Then I need to refer to that number in an expression like \1.

Dharman
  • 30,962
  • 25
  • 85
  • 135

2 Answers2

1

How about this?

(\[\d+\]|\[\d+-\d+\]|\[\d+(,\d+)*\])
Actually this can be even more siplified to : (\[\d+-\d+\]|\[\d+(,\d+)*\])

my @test = (  
    "[5,7,14]",  
    "[23]",  
    "[12-15]"  
);  

foreach my $val (@test) {  
    if ($val =~ /(\[\d+-\d+\]|\[\d+(,\d+)*\])/ ) {  
        print "match $val!\n";  
    }  
    else {  
        print "no match!\n";  
    }  
}   

This prints:

match [5,7,14]!  
match [23]!  
match [12-15]! 

Whitespaces are not taken into account but you can add them if you need to

Jim
  • 18,826
  • 34
  • 135
  • 254
0

I think Jim's Answer is helpful, but some generalizing and coding for better understand:

  • If Questions was looking for more complex but possible one like [1,3-5]:

    (\[\d+(,\s?\d+|\d*-\d+)*\])
           ^^^^ optional space after ','
    //validates:
    [3,33-24,7]
    [3-34]
    [1,3-5]
    [1]
    [1, 2]
    

Demo for this Regex

JavaScript code for replacing digits by links:

//define input string:
var mytext = "[3,33-24,7]\n[3-34]\n[1,3-5]\n[1]\n[1, 2]" ;

//call replace of matching [..] that calls digit replacing it-self
var newtext = mytext.replace(/(\[\d+(,\s?\d+|\d*-\d+)*\])/g ,
    function(ci){ //ci is matched citations `[..]`
        console.log(ci);
        //so replace each number in `[..]` with custom links
        return ci.replace(/\d+/g, 
            function(digit){
                return '<a href="/'+digit+'">'+digit+'</a>' ;
            });
    });
console.log(newtext);

/*output:
'[<a href="/3">3</a>,<a href="/33">33</a>-<a href="/24">24</a>,<a href="/7">7</a>]
[<a href="/3">3</a>-<a href="/34">34</a>]
[<a href="/1">1</a>,<a href="/3">3</a>-<a href="/5">5</a>]
[<a href="/1">1</a>]
[<a href="/1">1</a>, <a href="/2">2</a>]'
*/
MohaMad
  • 2,575
  • 2
  • 14
  • 26
  • Thanks for all the answers, and the thought that went into them. I searched for the previously submitted answer, per Wiktor's comment, but haven't found it yet... – Peter Basch Mar 04 '17 at 18:24
  • I don't think I made it clear that I'm trying to extract each one-to-three digit number, separately. – Peter Basch Mar 04 '17 at 18:29
  • When you find `[1, 2]` with regex, try splitting it and parsing your links. example code for this replacement added to my answer. I hope it help you. @PeterBasch – MohaMad Mar 05 '17 at 15:58
  • Thanks for your help. As I'm exploring this problem, I'm getting better at asking for help. I reposted my question, better phrased, here: http://stackoverflow.com/questions/42603140/regex-for-n-1-to-3-digit-numbers-in-square-brackets-with-commasspaces-between/42605379#42605379 – Peter Basch Mar 06 '17 at 23:52