Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hello, I`m having problems with Regex.
I have a line
Contract Nr.123456,reg.Nr.654321-118


I want to use a regex that Finds only the string 123456, but doesnt find the second 6 digit- 3 digit string(654321-118)

This is what i came up with, but dont really know what to do next
C#
string regex4 = @"\d{4,6}[^-]";


Any Ideas?
Thank you.
Posted
Updated 30-Jun-13 11:33am
v2
Comments
Zoltán Zörgő 30-Jun-13 17:36pm    
Well, if the comma is specific, use it:
@"(\d{4,6}),";
Soloveikok 30-Jun-13 17:38pm    
the comma isnt specific, I think I need to build the regex so It didnt find strings that end with the "-" sign
Zoltán Zörgő 30-Jun-13 17:43pm    
Ok, but comma is no "-" either. So you need something more specific. Is it the first number in the line?
Soloveikok 30-Jun-13 17:57pm    
This is payment details in the bank,field-recievers info. There are two possible sets of digits xxxxxx and xxxxxx-xxx, I need to find only the first one.

 
Share this answer
 
Comments
Espen Harlinn 30-Jun-13 17:45pm    
Nice set of links :-D
Maciej Los 30-Jun-13 17:53pm    
Thank you, Espen ;)
Soloveikok 30-Jun-13 17:55pm    
Thats proboly how you got those 61.8k posts, by spamming links. Do you think I didnt go throo google before writing here, jesus If you dont want to helpme just dont.
Maciej Los 30-Jun-13 18:14pm    
Where are your manners? Do you want to be reported as a Abusive/Troll?

Don't forget to be more specific if you want to get detailed answer.
Soloveikok 1-Jul-13 2:02am    
Sorry, that was late night and I was stuck with that regex for a while.
Hello,

You need to look at the regex pattern x(?!y)

see here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp[^]

For example x(?|y) means "match x but not when it is followed by y"

So in your case:

Match a 4 to 6 number long string, but not when it is followed by a 0 to 2 digit string a dash and 3 digits:

\d{4,6}(?!\d{0,2}-\d{3})

When I am stuck with Regex a good resource I frequently use to debug my regex expressions is:
http://regexpal.com/[^]

Hope it helps.

Valery.
 
Share this answer
 
Comments
Soloveikok 30-Jun-13 18:14pm    
THANK YOU, It worked!
Soloveikok 10-Jul-13 9:14am    
any ideas how to make it not find strings as 123456-12345?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900