Search code examples
regexsublimetext3plaintext

ST3 regex to find number sequences not surrounded by other characters


Forgive the possibly question as I'm fairly new to formulating my own regex, and anything that I've come up with, I've done so in the past hour.

I have a huge text file that I'm working with in Sublime Text 3. I'm attempting to use regular expressions in ST's Search and Replace feature to identify all 2-4 digit sequences that are not surrounded by any other characters.

This would identify the following sequences:

01
330
0302

And ignore the following:

1
10405
3042-2
(3030
x5590

In the context of my document, a sequence not being surrounded by any other characters essentially means existing on its own line.

So in other words, if a 2-4 digit sequence exists on its own line, it needs to be identified. If not, or if it's <2 or >5 digits, it needs to be left alone.

The best I've been able to come up with so far is this:

\b\n[0-9]{2,4}\n\b

...which works for the identification of those sequences well enough, but doesn't actually seem to allow me to replace the identified area: clicking Replace or pressing the shortcut Ctrl+Shift+H only seems to highlight the sequences and then move on. If this is a problem with the regex I am using, what else can I use?


Solution

  • I'm not too clear on what you are asking, but if you want to match 2-4 digits on a line by itself, this should do it

    ^[0-9]{2,4}$
    

    The ^ matches begin of line, $ matches end

    If you want to allow for leading or trailing spaces, you can do this

    ^[ ]*[0-9]{2,4}[ ]*$
    

    What do you want to replace the text with? If say you want delete the matched text and the whole line, you can do this

    ^[0-9]{2,4}$\n
    

    This matches the entire line including the newline.

    Alternatively, you can capture the matched text with parentheses

    ^([0-9]{2,4})$
    

    and do a replace like this

    foo$1bar
    

    The $1 is whatever is in the (...). This will give you

    foo01bar
    foo330bar
    foo0302bar
    
    And ignore the following:
    
    1
    10405
    3042-2
    (3030
    x5590