473,386 Members | 1,924 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Regex. How can this match?

Hi,

I have written a regular expression which is supposed to pull a
direction (forward or reverse) designation from a file name.

Unfortunately, the direction designation can either be the whole word
("Forward" or "Reverse") or just a single letter ("F" or "R") and the rest
of the name is not as consistent as I would like.. For example
"P1|1_G10_Forward_primer.ab1" or "K8_I1_A01_F.ab1".

At the time I am processing the file names, I have already stripped off
the extension.

I have written the Regular Expression
public static Regex DirectionFromFIleName = new
Regex("_(?<Direction>[Forward_|Reverse_|R$|F$])");

This looks for the underscore, followed by "Forward" or "Reverse" or an "F"
as the last character in the string or an "R" as the last character in the
string, or so I thought.

In fact,

when Designation = "P1|1_G10_Forward_primer"
RegexLibrary.DirectionFromFIleName.Match(Designati on).Groups["Direction"].Value
= "F"!

How can it pick up that F when it is not the last character? I assume it has
something to do with putting the $ inside the square brackets, but I can't
figure out exactly what it is.

I can figure out a bunch of different work arounds for this, but I would
like to understand what the regular expression is doing for the future.

Thanks!
Ethan


Jul 5 '07 #1
3 1631
Ethan,

For this, I don't know that I would use that logic. I would parse apart
the parts of the filename by non-alphanumeric characters and then look for
the word or letters in the remaining results. Basically, you would use the
regular expression pattern "\W" (for non-alphanumeric characters) and then
call the Split method on the regular expression, passing your string.

In the array of strings that is returned, look for Forward, Reverse, R
or F.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ethan Strauss" <ethan dot strauss at Promega dot comwrote in message
news:%2*****************@TK2MSFTNGP05.phx.gbl...
Hi,

I have written a regular expression which is supposed to pull a
direction (forward or reverse) designation from a file name.

Unfortunately, the direction designation can either be the whole word
("Forward" or "Reverse") or just a single letter ("F" or "R") and the rest
of the name is not as consistent as I would like.. For example
"P1|1_G10_Forward_primer.ab1" or "K8_I1_A01_F.ab1".

At the time I am processing the file names, I have already stripped off
the extension.

I have written the Regular Expression
public static Regex DirectionFromFIleName = new
Regex("_(?<Direction>[Forward_|Reverse_|R$|F$])");

This looks for the underscore, followed by "Forward" or "Reverse" or an
"F" as the last character in the string or an "R" as the last character in
the string, or so I thought.

In fact,

when Designation = "P1|1_G10_Forward_primer"

RegexLibrary.DirectionFromFIleName.Match(Designati on).Groups["Direction"].Value
= "F"!

How can it pick up that F when it is not the last character? I assume it
has something to do with putting the $ inside the square brackets, but I
can't figure out exactly what it is.

I can figure out a bunch of different work arounds for this, but I would
like to understand what the regular expression is doing for the future.

Thanks!
Ethan


Jul 5 '07 #2
* Ethan Strauss wrote, On 5-7-2007 18:15:
Hi,

I have written a regular expression which is supposed to pull a
direction (forward or reverse) designation from a file name.

Unfortunately, the direction designation can either be the whole word
("Forward" or "Reverse") or just a single letter ("F" or "R") and the rest
of the name is not as consistent as I would like.. For example
"P1|1_G10_Forward_primer.ab1" or "K8_I1_A01_F.ab1".

I have written the Regular Expression
public static Regex DirectionFromFIleName = new
Regex("_(?<Direction>[Forward_|Reverse_|R$|F$])");

This looks for the underscore, followed by "Forward" or "Reverse" or an "F"
as the last character in the string or an "R" as the last character in the
string, or so I thought.

How can it pick up that F when it is not the last character? I assume it has
something to do with putting the $ inside the square brackets, but I can't
figure out exactly what it is.

I can figure out a bunch of different work arounds for this, but I would
like to understand what the regular expression is doing for the future.
There's a few error's in your regex. Let me try to explain:

_ : Find a '_'
[Forward_|Reverse_|R$|F$] : followed by any letter in the
following group 'F','o','r','w'
... ... 's', 'e', '_', '|', 'R', '$'
'F'
(?<Direction>) : Capture these in a named group called
Direction

Of course this isn't what you wanted ;)

This should work better:

_(?<Direction>Forward_|Reverse_|R$|F$)

Just removing the [] would make things work. Now it reads:

_ : Find a '_'
Forward_|Reverse_|R$|F$ : Find either "Forward_", "Reverse_",
"R" followed by end of line,
"F" followed by end of line
(?<Direction>) : Capture these in a named group called
Direction

I present a course in Regular expressions and you've fallen into the
trap many of my students have before you. Be very sure what each kind of
brace, bracket etc means in which context.

() Group, Capture, Set options
[] Character set, any characters in there match
{} Quantifier
<Naming of groups, look around

Jesse
Jesse
Jul 5 '07 #3
Thanks Jesse,
I had actually figured it out and was about to post the answer, but you
beat me to it.
The Regex which ended up working as I wanted is
"_(?<Direction>Forward|Reverse|R$|F$)"

This is slightly different from what is below, but only because I
changed my mind about what characters to capture...
Ethan
"Jesse Houwing" <je***********@nospam-sogeti.nlwrote in message
news:uV**************@TK2MSFTNGP02.phx.gbl...
>* Ethan Strauss wrote, On 5-7-2007 18:15:
>Hi,

I have written a regular expression which is supposed to pull a
direction (forward or reverse) designation from a file name.

Unfortunately, the direction designation can either be the whole word
("Forward" or "Reverse") or just a single letter ("F" or "R") and the
rest of the name is not as consistent as I would like.. For example
"P1|1_G10_Forward_primer.ab1" or "K8_I1_A01_F.ab1".

I have written the Regular Expression
public static Regex DirectionFromFIleName = new
Regex("_(?<Direction>[Forward_|Reverse_|R$|F$])");

This looks for the underscore, followed by "Forward" or "Reverse" or an
"F" as the last character in the string or an "R" as the last character
in the string, or so I thought.

How can it pick up that F when it is not the last character? I assume it
has something to do with putting the $ inside the square brackets, but I
can't figure out exactly what it is.

I can figure out a bunch of different work arounds for this, but I would
like to understand what the regular expression is doing for the future.

There's a few error's in your regex. Let me try to explain:

_ : Find a '_'
[Forward_|Reverse_|R$|F$] : followed by any letter in the
following group 'F','o','r','w'
... ... 's', 'e', '_', '|', 'R', '$'
'F'
(?<Direction>) : Capture these in a named group called
Direction

Of course this isn't what you wanted ;)

This should work better:

_(?<Direction>Forward_|Reverse_|R$|F$)

Just removing the [] would make things work. Now it reads:

_ : Find a '_'
Forward_|Reverse_|R$|F$ : Find either "Forward_", "Reverse_",
"R" followed by end of line,
"F" followed by end of line
(?<Direction>) : Capture these in a named group called
Direction

I present a course in Regular expressions and you've fallen into the trap
many of my students have before you. Be very sure what each kind of brace,
bracket etc means in which context.

() Group, Capture, Set options
[] Character set, any characters in there match
{} Quantifier
<Naming of groups, look around

Jesse
Jesse

Jul 6 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Daniel Billingsley | last post by:
First, if MSFT is listening I'll say IMO the MSDN material is sorely lacking in this area... it's just a whole bunch of information thrown at you and you're left to yourself as to organizing it in...
3
by: Jeff McPhail | last post by:
I am using Regex.Match in a large application and the memory is growing out of control. I have tried several ways to try and release the memory and none of them work. Here are some similar examples...
20
by: jeevankodali | last post by:
Hi I have an .Net application which processes thousands of Xml nodes each day and for each node I am using around 30-40 Regex matches to see if they satisfy some conditions are not. These Regex...
17
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher ...
5
by: Kofi | last post by:
Any takers? Got a string of DNA as an input sequence GGATGGATG, apply the simple regex "GGATG" as in Regex r = new Regex("GGATG", (RegexOptions.Compiled)); MatchCollection matches =...
3
by: spamsickle | last post by:
I have a Perl background, so some of what I know in other contexts is probably getting in the way of what I need to learn now. With that said, I'm having a problem getting my regex to work as I...
3
by: aspineux | last post by:
My goal is to write a parser for these imaginary string from the SMTP protocol, regarding RFC 821 and 1869. I'm a little flexible with the BNF from these RFC :-) Any comment ? tests= def...
1
by: jonnyboy6969 | last post by:
Hi All Really hoping someone can help me out here with my deficient regex skills :) I have a function which takes a string of HTML and replaces a term (word or phrase) with a link. The pupose...
1
by: al.moorthi | last post by:
the below program is working in Suse and not working on Cent 5: can any body have the solution ? #include <regex.h> #include <stdlib.h> #include <stdio.h> int main(){ char cool =...
4
by: seberino | last post by:
I'm looking over the docs for the re module and can't find how to "NOT" an entire regex. For example..... How make regex that means "contains regex#1 but NOT regex#2" ? Chris
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.