Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular expression 1

Status
Not open for further replies.

TheBugSlayer

Programmer
Sep 22, 2002
887
0
0
US
Hi all.

I need to find a regular expression that pulls the last 9 digits presented before a separator of -,/ or space.

Example:
123456789-012 should return 123456789
123456789ABC/123 should return 123456789
123456 should return 123456

Thanks in advance for your help.

MCP SQL Server 2000, MCTS SQL Server 2005, MCTS SQL Server 2008 (DBD, DBA)
 
there are plenty of regex builders to choose from
off the top of my head, it would probably look something like
Code:
[0-9]+[A-Za-z]*[,-/\s]
which will pull 1 or more digits followed by 0 or more characters followed by a comma, dash, slash or space. from this result you can extract the leading digits
Code:
^[0-9]+
pull the 1 or more digits starting at the first character.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
[0] Just to assure the reading is correct:

>the last 9 digits presented before a separator of -,/ or space.
I take it as 1 to 9 digits; separator -, /, or space (not including the comma).

>123456 should return 123456
I take it the end of string is also a "separator"/delimiter. To reconcile with the examples presented before this example, I take it after 012 or 123 there are things (not the end of the string).

[1] In that case, I would use this sketch.
[tt]
string input = "123456789ABC/123xxxyyysomething";
string pattern=@"\d{1,9}(?=($|[^ /\d-]*( |/|-)))"; //if $ is included.
Match m=Regex.Match(input, pattern, RegexOptions.RightToLeft);
string answer="";
if (m.Success) {
answer=m.Value;
}
//answer empty means no find.
[/tt]
 
Tsuji,

Thank you ver much. Your solution is very close. When I enter it in Espresso with this input:
Code:
1-123456789-012
2-123456789ABC/123
3-123456
4-123A456789123
5-123456789123
I get
Code:
for (1) 123456789 and 012 - Expected 123456789
for (2) 123456789 and 123 - Expected 123456789
for (3) 123456 - Expected 123456
for (4) 123, 456789123 - Expected 456789123 
for (5) 123456789 and 123 - Expected [COLOR=red]456789123[/color]

I don' t want anything that comes after the 9th digit. So 012, 123, 123 should not be considered. But that could be taken care of in code.

Thanks again for your help.

MCP SQL Server 2000, MCTS SQL Server 2005, MCTS SQL Server 2008 (DBD, DBA)
 
[2] That seems to confirm my suspicion that the spec of the requirement is actually incomplete. To reconcile the case (3) with cases (1) and (2), I can only impose that the numeric has digits longer than 3 just to eliminate the 3-digit results for (1) and (2). 4-9 digits is very contingent, stop-gap. You may tighten it up according to your need (nobody else really know). My pattern is conceived with considerably generality, in particular the substring at the start to the last matching pattern, because of the looseness of the description in the op.

[2.1] The revision should do, should it not?
[tt] string pattern=@"\d{[red]4[/red],9}(?=($|[^ /\d-]*( |/|-)))";[/tt]
 
Thanks TSuji...You are very, very close...The latest pattern yields
Code:
123456789
123456789
123456
456789123 
[COLOR=red]123456789123[/color] - should yield [COLOR=red][b]456789123[/b][/color]
It should return the last 9 digits, in other words we need to take 9 characters from the position of the separator - 1, not from the beginning of the string.

I am playing with the patter. You' ve been very helpful.

MCP SQL Server 2000, MCTS SQL Server 2005, MCTS SQL Server 2008 (DBD, DBA)
 
>123456789123 - should yield 456789123
It does, does it not?
 
Hey TSuji,

No, it does not. It yields the first 9 instead, not the last 9. If you' re coming up with this without testing it you' re truly a regular expression genious...:)

Thanks.

MCP SQL Server 2000, MCTS SQL Server 2005, MCTS SQL Server 2008 (DBD, DBA)
 
As I am not, I test it then. The result: it does.
 
One of the options tsuji specified is 'RightToLeft' (see post with timestamp: 18 Feb 11 2:18). Have you enabled this option in your test environment?
 
TSuji,

Once again, I appreciate you taking the time out to help. What I am saying to you is the result of the ONLY test I am doing, which is through Espresso. Please image attached (uploaded via with the result of my testing.

123456789123 should yield 456789123 per the requirements, it is returning 123456789 unless I am not seeing something. It should return the last 9, right to left. The 456789123 you see is the output from 123A456789123, which is correct. The last two inputs should yields 456789123.

Let me code it in C#...maybe that' s where the problem lies. Will post here when I do.

Thank you both.

MCP SQL Server 2000, MCTS SQL Server 2005, MCTS SQL Server 2008 (DBD, DBA)
 
TSuji and JGes,

Espresso is wrong, TSuji is write. Once coded in C# I am getting the right result.
Code:
 static void Main(string[] args)
 {
            List<string> ax = null;
            string pattern = @"\d{4,9}(?=($|[^ /\d-]*( |/|-)))";
            string searchString = @"123456789-012
                                    123456789ABC/123
                                    123456
                                    123A456789123
                                    123456789123";
            Regex rx = new Regex(pattern, RegexOptions.IgnoreCase);
            ax = (from Match x in rx.Matches(searchString) select x.Value).ToList<string>();
            if (ax.Count > 0)
            {
                foreach (string s in ax )
                {
                    Console.WriteLine(s);
                }
            }
            Console.ReadLine();
}

You should probably contact those guys...

Thanks a million.

MCP SQL Server 2000, MCTS SQL Server 2005, MCTS SQL Server 2008 (DBD, DBA)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top