3

I'm searching for strings matching the pattern [A-Z]\W*[0-9]+, so that in

V-2345
35A235
Q252

the V-2345 and Q252 would match. In another list, I want to find equivalent items that fit the same regex pattern, so that potential matches would include:

V ++ 2345
Q 252

but not

V-252
Q//2345

Basically, if an item matches the pattern in the first list, I want to search for that letter prefix and number suffix in the second list. Is there a term for using regex to do this kind of search? I know that I can just write my own search by using string manipulation to get the letter and number, then use them to compose a separate regex pattern for each search of the second list, but I'm wondering if there's something built into typical regex flavors (I'm using C# in this case) that serves this purpose so that I can just use the original pattern.

  • 1
    I'm pretty sure the answer is "no", since this is going well into programming language territory and regex is far from a programming language, but it's off-topic here since this site is about conceptual questions related to software design and development rather than concrete "How do I get X to do Y?" troubleshooting or code writing questions. – Ixrec Feb 8 '16 at 7:26
  • @Ixrec: first, the answer is not no, see mine. Second, this post can be interpreted as a general question about what is possible with regular expressions, which is clearly a conceptual one, at least in my eyes. – Doc Brown Feb 8 '16 at 9:03
  • @DocBrown Ah, somehow I forgot the possibility of putting the two lists in the same file so one regex could operate on them. – Ixrec Feb 8 '16 at 9:23
4

Regular expressions work on strings, not on a "string list" and not multiple string lists. Wherever you need to process more than one string, you will typically need some environmental code to do the processing. For your example, this code has to apply the regex to every element of the first list, then collect the results and use this results to process the second list.

Said that, the usual approach to apply a regexp to a list of strings is to concatenate them by a separator character like "newline". To concatenate two lists and distinguish them, you would need at least a special "magic" character or word for separating the first list from the second, which is not part of the list. Using such magic can cause some maintenance headaches if you are not very careful, nevertheless by combining this with backreferences, this can be used to solve your problem.

For example, numbered backreferences like \1 to \9 refer to other capturing groups found before. Lets assume you used "###" as a separator for the two lists, a regexp along the lines of

  ^([A-Z])\W*([0-9]+)$.*###.*^(\1\W*\+\+\W*\2)$
    ^         ^          ^     ^           ^
    |         |          |     |           |
   first    second       |    backrefs to first
   group    group        |    or second group
                       lists
                       separator

might be a first approximation for what you are looking for (beware of bugs, I did not test it). Put this into a global regexp search, then it should produce all pairs of matches which fit to your constraints.

As a final remark: the resulting code may be very compact, nevertheless harder to maintain (and probably slower) than a more explicit solution where you process the two lists individually.

  • I should have made clear in my OP that I'm iterating through a list of strings (in C#, that's List<String>, but my question was really meant to be language agnostic), performing the regex check on each string. – sigil Feb 9 '16 at 19:46
  • @sigil: sounds you don't know what you want - do you want to use your own search, or not? How do you intend to reuse the result from the first match and apply it to a second list in a regexp if you do not want to combine both lists first to one big string? – Doc Brown Feb 9 '16 at 21:53
  • Yes, I've been kind of confused about this. Learning more about capturing groups has been helpful, because what I want is to construct the regexp for the they let me construct the regexp for the second list more easily. – sigil Feb 10 '16 at 2:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.