0

I'm having a bit of trouble with my regex.

String a = @"{target=}jump";
String b = "continue";
String c = "jump";
String d = @"{target=intro}jump";
String e = "prev";
String f = @"{target=}choice";
String g = @"{target=intro}choice";
String h = "choice";
String i = "previous";
String j = @"{target=intro}continue";
String k = "cont";
String l = @"{target=}continue";
Regex regex = new Regex(@"(^{target=(\w.*)}(choice|jump))|(^[^.]*(continue|previous))");
var a_res = regex.IsMatch(a);
var b_res = regex.IsMatch(b);
var c_res = regex.IsMatch(c);
var d_res = regex.IsMatch(d);
var e_res = regex.IsMatch(e);
var f_res = regex.IsMatch(f);
var g_res = regex.IsMatch(g);
var h_res = regex.IsMatch(h);
var i_res = regex.IsMatch(i);
var j_res = regex.IsMatch(j);
var k_res = regex.IsMatch(k);
var l_res = regex.IsMatch(l);

Basically what i need is to get a match when choice or jump is present that it is proceeded by {target= } with any number of characters after the =. And also to match if continue or previous are present but only if they are proceeded by nothing.

so A = false, b = true, c = false, d = true, e = false, f = false, g = true, h = false, i = true, j = false, k = false and l = false,

with my regex above I get correct reading for everything bar j and l.

Can anyone please help?

2
  • you posted some answers that should be comments. I think you can still delete them and put them as comments. If I comment on the answers, you might not be able to delete them. Commented Dec 21, 2010 at 14:58
  • Actually, you can delete your answer regardless, I was confusing the procedure for deleting questions with the procedure for deleting answers. Commented Dec 21, 2010 at 19:30

2 Answers 2

1

I think you want to replace

{target=(\w.*)}

with

{target=[^}]*}

This allows for any number of non-} characters after the "target=".

EDIT: Actually, could you please clarify your meaning? I gather from context that

"A = false, b = true, c = false, d = true, e = false, f = false, g = true, h = false, i = true, j = false, k = false and l = false"

are the expected values (rather than actual.. you should mention this explicitly for greater clarity). But based on your verbal description, l (correction: a and f) should be true, not false.

Also, I was going on a "fix one error at a time" basis. My regex still gives j true even though it should be false based on your verbal description. I'll make another edit shortly.

EDIT 2: I believe what you are after is

^({target=[^}]+}(choice|jump)|continue|previous)

EDIT 3: Sorry to edit so many times, but I should mention that taking the first } after the = is an assumption (albeit a common and often practical one). A more comprehensive test suite would indicate whether

{target=}}choice

should be a match.

Sign up to request clarification or add additional context in comments.

2 Comments

doesnt this also match 0 occurences though?
@Shannow 0 is a number. If you want to disallow 0 you have to say so, as in "any nonzero number of characters".
0

Similar to Mitch's answer, but:

{target=[^}]+}

This should match at least 1 non } character.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.