0

I have the following String :

var resultLine= "[UT] - GSM incoming call : STEP 1 - Simulate reception from server (1)Rerun3713 msAssertion ok"

And the following code which is responsible to check of the String matched with the Regex :

var resultRE = /^([ \w-]*: )?(.+) \((\d+), (\d+), (\d+)\)Rerun/;
var resultMatch = resultLine.match(resultRE);
if (resultMatch) {
   return true;
} else {
   return false;
}

In this case, i have an error in my Regex because i always get "false". Where is my mistake ?

4
  • 3
    Your string bears almost no resemblance to the pattern. Why would you ever expect that to match? Commented Sep 24, 2014 at 18:20
  • 1
    For starters, it appears that you are trying to find something with (NUM, NUM, NUM)Rerun in it, but your source string has only (NUM)Rerun. Commented Sep 24, 2014 at 18:20
  • 4
    It helps if you explain what exactly the pattern is supposed to be. Commented Sep 24, 2014 at 18:20
  • 1
    (1) != \((\d+), (\d+), (\d+)\) Commented Sep 24, 2014 at 18:23

3 Answers 3

1

I would recommend the following pattern based on what it appears you are looking for:

var resultRE = /^([\[ \w\]-]*: )(.+) \(([0-9, ]*)\)Rerun(.*)$/

This should force all capture groups to exist, even if they are empty, and will allow for multiple numbers before Rerun as you seem to expect.

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

1 Comment

In your case you really do need to escape the dash in the character class (since it's being interpreted as a range here). Since the OP was so vague, I don't know if it matters, but the first group will match things like "UT[[][]". However, this fits what was provided.
1

This matches nothing in your string

([ \w-]*: )?

Since it was optional, that doesn't matter because it gets caught by the all inclusive

(.+)

If you were trying to match the [UT] part with it's separator, it would look something like this

(\[\w+\][\s\-]*)?

As noted in the comments, you only have one number in parentheses but your regex requires three sets of them, separated by commas. This will allow any number of numbers, separated by commas indefinitely (I don't know if there's a limit or not).

\((\d+,\s)*(\d+)\)

If you need something more specific, you'll have to be more specific about what template your matching, not a specific case. But the best I can figure with what you've provided is

^(\[\w\][\s\-]*)?(.+)\((\d+,\w)*(\d+)\)Rerun

4 Comments

I think your second regexp might need to have the dash and optional spacing outside the escaped [ and ], if I'm reading the source line correctly...
I don't think the dash needs to be escaped being on the end but at the very least, you're right it should be. As far as the brackets, they're escaped, I just forgot to remove the colon. Thank for the input
I just meant that the dash in the line to be matched appears outside the [ and ] surrounding the UT portion... Maybe something like (\[\w\]\s*-)??
Oh, I see what you mean. I didn't notice why the dash was there to begin with. I guess it really wouldn't make sense to allow the line to start with " - ".
0
var resultRE = /\((\d+)(?:, (\d+))?(?:, (\d+))?\)Rerun/;
if (resultRE.test(resultLine)) {
  var num1 = RegExp.$1,
  num2 = RegExp.$2,
  num3 = RegExp.$3;
}

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.