tags:

views:

2120

answers:

6

Imagine that users are inserting strings in several computers.

On one computer, the pattern in the configuration will extract some characters of that string, lets say position 4 to 5. On another computer, the extract pattern will return other characters, for instance, last 3 positions of the string.

These configurations (the Regex patterns) are different for each computer, and should be available for change by the administrator, without having to change the source code.

Some examples:

         Original_String       Return_Value
User1 -  abcd78defg123         78
User2 -  abcd78defg123         78g1
User3 -  mm127788abcd          12
User4 -  123456pp12asd         ppsd

Can it be done with Regex? Thanks.

+6  A: 

Why do you want to use regex for this? What is wrong with:

string foo = s.Substring(4,2);
string bar = s.Substring(s.Length-3,3);

(you can wrap those up to do a bit of bounds-checking on the length easily enough)

If you really want, you could wrap it up in a Func<string,string> to put somewhere - not sure I'd bother, though:

Func<string, string> get4and5 = s => s.Substring(4, 2);
Func<string,string> getLast3 = s => s.Substring(s.Length - 3, 3);
string value = "abcd78defg123";
string foo = getLast3(value);
string bar = get4and5(value);
Marc Gravell
I didn't explain myself. Sorry for that. I want to have all these patterns in a table that applies to a particular case.
Nelson Reis
I'm pretty certain you could think of a simple syntax to express the above - [4,2] and [-3,3] for example (where - means "from the right")
Marc Gravell
It's about being configurable without having to recompile.
Greg Domjan
Yes the problem is about flexibility in the configuration. I'm not sure this can be done in Regex, but would help a lot!
Nelson Reis
+1  A: 

I'm not sure what you are hoping to get by using RegEx. RegEx is used for pattern matching. If you want to extract based on position, just use substring.

kgrad
+1  A: 

It seems to me that Regex really isn't the solution here. To return a section of a string beginning at position pos (starting at 0) and of length length, you simply call the Substring function as such:

string section = str.Substring(pos, length)
Noldorin
+1  A: 

Grouping. You could match on /^.{3}(.{2})/ and then look at group $1 for example.

The question is why? Normal string handling i.e. actual substring methods are going to be faster and clearer in intent.

annakata
+3  A: 

If you really want to use regex:

^...(..)

And:

.*(...)$
Gumbo
.* is unnecessary in your regex #2. There is need to even look at anything before the last three characters.
Tomalak
It’s unnecessary but reduces backtracking. Now it expands the first expression (`.*`) to the whole string and then backtracks only the last three characters to get a match for the whole expression. Otherwise it would check on each character if the end is already reached.
Gumbo
Your answer was a part of the result I would like to achive. Thanks.
Nelson Reis
+1  A: 

To have a regex capture values for further use you typically use (), depending on the regex compiler it might be () or for microsoft MSVC I think it's []

Example

User4 -  123456pp12asd         ppsd

is most interesting in that you have here 2 seperate capture areas. Is there some default rule on how to join them together, or would you then want to be able to specify how to make the result?

Perhaps something like

r/......(..)...(..)/\1\2/  for ppsd
r/......(..)...(..)/\2-\1/ for sd-pp

do you want to run a regex to get the captures and handle them yourself, or do you want to run more advanced manipulation commands?

Greg Domjan
This is what I was looking for! In C# I don't think you can specify how the result would return, I can only apply this pattern ^......(..)...(..)$ and in that case I will have to handle each match.Group[x] myself. Thanks a lot!
Nelson Reis
Hey, but if you know a way of handling all the matches in C#, let me know!
Nelson Reis