1

I am new to C and I am trying to detect if a user entered some regex of IntxInt (i.e. 2x5 or 10x15). I will not go over 15.

From what I have gathered I can just make a regex to detect this. I have been confused on making regex's for C though and no examples have been very useful yet.

I found this example here

string pattern = @"\*\d*\.txt";
Regex rgx = new Regex(pattern)
input = rgx.Replace(input, "");

And my guess of making it fit the above criteria would be something like this

string pattern = @"[0-9][0-9]*[x|X][0-9][0-9]*";

I would guess this as I need at least 1 digit, followed by possible another? Not sure how to limit it to 0 or 1 more numbers. Followed by an X. Then the same thing as the first part.

Is this right/wrong, why?

If this is correct, how do I "test" the input I get in?

Unihedron
  • 10,902
  • 13
  • 62
  • 72
Austin
  • 3,010
  • 23
  • 62
  • 97
  • 1
    Your example looks like `C++`, not `C`. – AntonH Sep 22 '14 at 13:22
  • @AntonH *facepalm* :( really? Is my regex close at all? – Austin Sep 22 '14 at 13:23
  • I've been out of the loop for a while, but as far as I know regex was only introduced in C++ 11. – Gigi Sep 22 '14 at 13:24
  • It looks like `C#`, now that I take a closer look, actually. That aside, Are you looking for `(digits)` `(x or X)` `(digits)`? – Unihedron Sep 22 '14 at 13:24
  • @Gigi I see...so how would I do this then? – Austin Sep 22 '14 at 13:25
  • @Unihedron Yes. I want to find all input ranging from 1x1 to 15x15 as valid input. They are game coordinates. – Austin Sep 22 '14 at 13:25
  • @AntonH Yea, despite my brand newness of C, I should have realized that. :/ Any hints on how to go about this then? – Austin Sep 22 '14 at 13:26
  • regex does exist in `C`, but it's an extermnal library. – AntonH Sep 22 '14 at 13:26
  • so should I just check each char? like in Java something like input[0] = int? input[1] = int or x... – Austin Sep 22 '14 at 13:27
  • @Austin This question addresses regex in C: http://stackoverflow.com/questions/1085083/regular-expressions-in-c-examples external like also: http://www.lemoda.net/c/unix-regex/ – AntonH Sep 22 '14 at 13:28
  • @Unihedron is right - unless they added string literals with the `@` since I last used C/C++, it's got to be C#. Austin, can you at least confirm what language you're using? – Gigi Sep 22 '14 at 14:49
  • @Gigi `@" "` is `CString` notation, so it's likely C#. Since C++ was added by another user, I've edited in C# instead. – Unihedron Sep 22 '14 at 14:50

1 Answers1

0

Use this regex:

^(?i)(?:1[0-5]|[1-9])x(?:1[0-5]|[1-9])$
  • (?i) makes x case-insensitive so it can match both x and X.
  • (?:1[0-5]|[1-9]) matches digits from "1" to "15".

Here is a regex demo.

Unihedron
  • 10,902
  • 13
  • 62
  • 72
  • awesome! Same comment I added to the other guy. Will this work in C, or do I need to import something like everyone else said? – Austin Sep 22 '14 at 13:29
  • You can look at [this SO thread](http://stackoverflow.com/q/1085083/3622940) about it @Austin, `CString` is for C++ – Unihedron Sep 22 '14 at 13:32
  • Cool! I think the labs here use POSIX, which SO is saying will work with regex. So how I do I test my regex to the input? i.e. I have `scanf("%d", &a);`, so how do I test it to this? then I can reply back to let you know if it worked! :) – Austin Sep 22 '14 at 13:34
  • @Austin I personally use [PCRE](http://www.pcre.org/) because it's easier. But I'll try to show working C code demo. – Unihedron Sep 22 '14 at 13:35
  • Okay, well we have to make sure our final program will compile on the machines here at my university. So I am kind of limited to the prof's standards. I figured regex would be a neat addition hence why I am trying to see if it will work. – Austin Sep 22 '14 at 13:36