tags:

views:

100

answers:

4

Writing a quick app to help me filter text files.

I'm reading in a text file line-by-line, and need to match a series of characters that looks like this: 090129 YBB 100

The first set, 090129, will be 6 numbers (0-9). Followed by a space, and then YBB - always. After that, another space, then 2-3 numbers (0-9).

This pattern will always be the first part of the string as well.

Here's my hack at it:

^[0-9][0-9][0-9] (YBB) [0-9][0-9][0-9]\b

Of course, doesn't work... but I'm a regex noob. Thanks in advance!

+5  A: 

Here goes:

^([\d]{6})\s(?:YBB)\s([\d]{2,3})\b

Explanation:

a) Start at start of line. b) Match 6 digits. Save into backref 1. c) Match a space. d) Match "YBB". Don't save into backref. e) Match a space. f) Match 2-3 digits. Save into backref 2.

Of course, it's important to know which part of this pattern you want to retrieve into a backreference. If you provide that info, I can edit my post.

Cerebrus
I'm assuming you want to retrieve the digital part of the pattern. Edited my post and explanation.
Cerebrus
That works great. Thank you very much for your time.
Chad
You're most welcome and it's my pleasure. :-)
Cerebrus
+2  A: 

In Perl, I'd do:

^(\d{3}) YBB (\d{2,3})$
Andrew Medico
I'm using C#, but thanks for the PERL code too.
Chad
A: 

This webtool might be able to help you: http://www.txt2re.com/

For this case in particular: http://www.txt2re.com/index-javascript.php3?s=090129%20YBB%20100&-23&12&19&13&16&18&20&7&-3&8&17&14&15

Vordreller
awesome site, thank you!
Chad
A: 

You say there will be 6 digits before YBB, but the regex you show has only 3.

Anonymous Coward
lol, true that! oh man, I'm bad at regex. Thanks for pointing that out.
Chad