简体   繁体   中英

Regex to find an ID code within an email subject line

We are implementing a feature in our web application that reads email subjects and checks the subject for a certain string containing an ID so that we can match incoming emails to projects in our application.

A subject may look like this: Stand-by vessel requirement 07.03.2013 [FIX:1012]

I need to identify the string in square brackets and pull out just the number from it: '1012'

So far i have come up with this:

\[[a-z]{5}:[0-9]{4}\]\Z

But this only matches the whole string : [FIX:1012]

Where do i go from here ?

Use look-behind and look-ahead:

(?<=\[[a-z]{5}:)[0-9]{4}(?=\]\Z)

?<= is look-behind and ?= is look-ahead.

Using groups:

Just putting it in brackets to put it in the first group:

\[[a-z]{5}:([0-9]{4})\]\Z

To use groups would be something like (untested):

Match match = regex.Match(input);
if (match.Success)
{
    string v = match.Groups[1].Value;
    Console.WriteLine("{0}", v);
}

Reference .

Also:

I'm not really sure how [az]{5} works for your string (this is only exactly 5 lower-case characters).

Here is your regex code :

     [0-9]+(?:\.[0-9]*)?+(:*?])

This will give you 1012 number which is between " : " " ] "

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM