Highlighted

Regular expression problem

Advocate ,
Jun 15, 2016

Copy link to clipboard

Copied

Dear friends,

In my script i have some sections which test the content of an edit field before it is processed further.

Things like the following work perfectly:

var re_Def = /#[A-Za-z][A-Za-z0-9_]+/;          // valid variable name ?
items = ["#correct", "notcorrect", "#This_is4", "#thisIs", "@something", "#ALLOK", "", ];
// search    0            -1          -1!!        -1!!          -1        -1!!     -1      <--- incorrect method
// test    true         false         true        true        false       true    false    <--- correct method
for (var j = 0; j < items.length; j++) {
  var item = items;
  alert ("'" + item + "' ==> " +  item.search(re_Def) + "\n" + re_Def.test(item));
}
var re_Def = /(\[ROW +\d+\]|\[COL +\d+\]|\[CELL +\d+, +\d+\]|Left *\(\d*\)|Right *\(\d*\)|Above *\(\d*\)|Below *\(\d*\))/;
items = ["[ROW 17]", "[Row n]", "[ROW n]", "[CELL 3, 9]", "[CELL 3 9]", "Abbove()", "Right(3)"];
// result  true        false      false         true         false        false         true   
for (var j = 0; j < items.length; j++) {
  alert ("'" + items + "' ==> " +  re_Def.test(items));
}

But the following always return false, independly of the content of the string item:

var re_Def = /{[EFJ]\d*}|{I}/;    // valid format def?
var item = "{E27}";
var result = re_Def.test(item);
alert (result);                   // false !!

RegEx Buddy tells me, that
- the REGEX is correct
- the result should be true, not false

- The verbose defintion of the RegEx is:
Match either the regular expression below (attempting the next alternative only if this one fails) «{[EFJ]\d*}»
   Match the character “{” literally «{»
   Match a single character present in the list “EFJ” «[EFJ]»
   Match a single digit 0..9 «\d*»
      Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
   Match the character “}” literally «}»
Or match regular expression number 2 below (the entire match attempt fails if this one fails to match) «{I}»
   Match the characters “{I}” literally «{I}»

Unrecognised typo? Faulty method test?

Results are wrong as soon as i use character list [...] - but look at the first code block: there are also character lists which are handled correctly.

The curly brackets in the regular expression need to be escaped to be taken literally:

var re_Def = /\{[EFJ]\d*\}/;

Kind regards

JoH

TOPICS
Scripting

Views

190

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Regular expression problem

Advocate ,
Jun 15, 2016

Copy link to clipboard

Copied

Dear friends,

In my script i have some sections which test the content of an edit field before it is processed further.

Things like the following work perfectly:

var re_Def = /#[A-Za-z][A-Za-z0-9_]+/;          // valid variable name ?
items = ["#correct", "notcorrect", "#This_is4", "#thisIs", "@something", "#ALLOK", "", ];
// search    0            -1          -1!!        -1!!          -1        -1!!     -1      <--- incorrect method
// test    true         false         true        true        false       true    false    <--- correct method
for (var j = 0; j < items.length; j++) {
  var item = items;
  alert ("'" + item + "' ==> " +  item.search(re_Def) + "\n" + re_Def.test(item));
}
var re_Def = /(\[ROW +\d+\]|\[COL +\d+\]|\[CELL +\d+, +\d+\]|Left *\(\d*\)|Right *\(\d*\)|Above *\(\d*\)|Below *\(\d*\))/;
items = ["[ROW 17]", "[Row n]", "[ROW n]", "[CELL 3, 9]", "[CELL 3 9]", "Abbove()", "Right(3)"];
// result  true        false      false         true         false        false         true   
for (var j = 0; j < items.length; j++) {
  alert ("'" + items + "' ==> " +  re_Def.test(items));
}

But the following always return false, independly of the content of the string item:

var re_Def = /{[EFJ]\d*}|{I}/;    // valid format def?
var item = "{E27}";
var result = re_Def.test(item);
alert (result);                   // false !!

RegEx Buddy tells me, that
- the REGEX is correct
- the result should be true, not false

- The verbose defintion of the RegEx is:
Match either the regular expression below (attempting the next alternative only if this one fails) «{[EFJ]\d*}»
   Match the character “{” literally «{»
   Match a single character present in the list “EFJ” «[EFJ]»
   Match a single digit 0..9 «\d*»
      Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
   Match the character “}” literally «}»
Or match regular expression number 2 below (the entire match attempt fails if this one fails to match) «{I}»
   Match the characters “{I}” literally «{I}»

Unrecognised typo? Faulty method test?

Results are wrong as soon as i use character list [...] - but look at the first code block: there are also character lists which are handled correctly.

The curly brackets in the regular expression need to be escaped to be taken literally:

var re_Def = /\{[EFJ]\d*\}/;

Kind regards

JoH

TOPICS
Scripting

Views

191

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jun 15, 2016 0
Explorer ,
Jun 15, 2016

Copy link to clipboard

Copied

The curly brackets in the regular expression need to be escaped to be taken literally:

var re_Def = /\{[EFJ]\d*\}/;

Kind regards

JoH

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 15, 2016 0
K.Daube LATEST
Advocate ,
Jun 15, 2016

Copy link to clipboard

Copied

Thank You JoH,

Seems to be an error in RegexBuddy - as it accepts both escaped and unescaped braces here.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 15, 2016 0