Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #14726 > unrolled thread

reg expression

Started by"phil hunt" <a@abc.com>
First post2012-06-27 17:48 -0400
Last post2012-06-28 12:08 -0400
Articles 13 — 7 participants

Back to article view | Back to comp.lang.javascript


Contents

  reg expression "phil hunt" <a@abc.com> - 2012-06-27 17:48 -0400
    Re: reg expression Bjoern Hoehrmann <bjoern@hoehrmann.de> - 2012-06-27 23:55 +0200
      Re: reg expression Dr J R Stockton <reply1226@merlyn.demon.co.uk.not.invalid> - 2012-06-28 19:08 +0100
        Re: reg expression Spamless <Spamless@Nil.nil> - 2012-06-29 08:22 -0500
          Re: reg expression "Phil Hunt" <aaa@aaa.com> - 2012-06-29 10:19 -0400
            Re: reg expression Spamless <Spamless@Nil.nil> - 2012-06-29 11:36 -0500
            Re: reg expression Gene Wirchenko <genew@ocis.net> - 2012-06-29 10:05 -0700
    Re: reg expression Gene Wirchenko <genew@ocis.net> - 2012-06-27 19:38 -0700
      Re: reg expression Gene Wirchenko <genew@ocis.net> - 2012-06-27 21:18 -0700
      Re: reg expression "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2012-06-28 06:24 +0000
        Re: reg expression Spamless <Spamless@Nil.nil> - 2012-06-28 05:36 -0500
          Re: reg expression "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2012-06-28 15:15 +0000
            Re: reg expression "Phil Hunt" <aaa@aaa.com> - 2012-06-28 12:08 -0400

#14726 — reg expression

From"phil hunt" <a@abc.com>
Date2012-06-27 17:48 -0400
Subjectreg expression
Message-ID<jsfv2r$sks$1@speranza.aioe.org>
In Jsctipt, I have

re = new RegExp("one|two", "gi");

"one three".search(re);  return an integer >-1, meaning it found a match

My question is how do I set up the RegExp to match "one" AND "two". I though 
an & would do it, but no.

Thanks in advance

[toc] | [next] | [standalone]


#14727

FromBjoern Hoehrmann <bjoern@hoehrmann.de>
Date2012-06-27 23:55 +0200
Message-ID<370nu797jqo96n2cai31am4j64ac7j0uqv@hive.bjoern.hoehrmann.de>
In reply to#14726
* phil hunt wrote in comp.lang.javascript:
>In Jsctipt, I have
>
>re = new RegExp("one|two", "gi");
>
>"one three".search(re);  return an integer >-1, meaning it found a match
>
>My question is how do I set up the RegExp to match "one" AND "two". I though 
>an & would do it, but no.

JavaScript regular expressions do not support intersections, you would
have to use several regular expressions (or compute the intersection in
some other tool).
-- 
Björn Höhrmann · mailto:bjoern@hoehrmann.de · http://bjoern.hoehrmann.de
Am Badedeich 7 · Telefon: +49(0)160/4415681 · http://www.bjoernsworld.de
25899 Dagebüll · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/ 

[toc] | [prev] | [next] | [standalone]


#14756

FromDr J R Stockton <reply1226@merlyn.demon.co.uk.not.invalid>
Date2012-06-28 19:08 +0100
Message-ID<H6FOnrFD2J7PFwND@invalid.uk.co.demon.merlyn.invalid>
In reply to#14727
In comp.lang.javascript message <370nu797jqo96n2cai31am4j64ac7j0uqv@hive
.bjoern.hoehrmann.de>, Wed, 27 Jun 2012 23:55:25, Bjoern Hoehrmann
<bjoern@hoehrmann.de> posted:

>* phil hunt wrote in comp.lang.javascript:
>>In Jsctipt, I have
>>
>>re = new RegExp("one|two", "gi");
>>
>>"one three".search(re);  return an integer >-1, meaning it found a match
>>
>>My question is how do I set up the RegExp to match "one" AND "two". I though
>>an & would do it, but no.
>
>JavaScript regular expressions do not support intersections, you would
>have to use several regular expressions (or compute the intersection in
>some other tool).

Not necessarily.    re = new RegExp("one.*two|two.*one", "gi");

But should "oneight" be considered to contain both "one" & "eight" ?

-- 
 (c) John Stockton, nr London UK  ?@merlyn.demon.co.uk  IE8 FF8 Op11 Sf5 Cr15
   news:comp.lang.javascript FAQ <http://www.jibbering.com/faq/index.html>.
   <http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
   <http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

[toc] | [prev] | [next] | [standalone]


#14761

FromSpamless <Spamless@Nil.nil>
Date2012-06-29 08:22 -0500
Message-ID<c4udnXrzrbCwMXDSnZ2dnUVZ_tKdnZ2d@inch.com>
In reply to#14756
On 2012-06-28, Dr J R Stockton <reply1226@merlyn.demon.co.uk.not.invalid> wrote:
> In comp.lang.javascript message <370nu797jqo96n2cai31am4j64ac7j0uqv@hive
> .bjoern.hoehrmann.de>, Wed, 27 Jun 2012 23:55:25, Bjoern Hoehrmann
><bjoern@hoehrmann.de> posted:
>
>>* phil hunt wrote in comp.lang.javascript:
>>>In Jsctipt, I have
>>>
>>>re = new RegExp("one|two", "gi");
>>>
>>>"one three".search(re);  return an integer >-1, meaning it found a match
>>>
>>>My question is how do I set up the RegExp to match "one" AND "two". I though
>>>an & would do it, but no.
>>
>>JavaScript regular expressions do not support intersections, you would
>>have to use several regular expressions (or compute the intersection in
>>some other tool).
>
> Not necessarily.    re = new RegExp("one.*two|two.*one", "gi");
>
> But should "oneight" be considered to contain both "one" & "eight" ?
>

How about this? I am assuming these are disjoint strings
(as someone noted - do you want to see a match of "one" and
"eleven" in the string "oneleven"?) or at least can only appear
disjointly.

Do a global match on the alternation/disjunction of the
strings, 'one|two" and see how many (distinct) matches
you get
   str.match(/one|two/g).length
will give the number of matches (if it's two, you matched
two times - but maybe you matched "one" twice!).

If you want to count the number of distinct matches ... well,
it is easier to count the number of distinct elements of an
array if it is sorted, so ...

LOOKING FOR "one" and/or "two" IN A STRING, str:
------------------------------------------------
   str="onetwothreetwoone";

   old="";count=0;
   str.match(/one|two/g).sort().forEach(function f(x){(x != old) && count++; old=x});
   // count is the result - test it.

"count" is the number of different matches.
If it is two you matched both.

[toc] | [prev] | [next] | [standalone]


#14763

From"Phil Hunt" <aaa@aaa.com>
Date2012-06-29 10:19 -0400
Message-ID<jske1l$c6k$1@speranza.aioe.org>
In reply to#14761
My requirement deos not care how many times or where the string may occurs.

I have another question. When the patern contains '(' it get an exception.
How do I escape that and other character that may give me trouble.

Thanks


"Spamless" <Spamless@Nil.nil> wrote in message 
news:c4udnXrzrbCwMXDSnZ2dnUVZ_tKdnZ2d@inch.com...
> On 2012-06-28, Dr J R Stockton <reply1226@merlyn.demon.co.uk.not.invalid> 
> wrote:
>> In comp.lang.javascript message <370nu797jqo96n2cai31am4j64ac7j0uqv@hive
>> .bjoern.hoehrmann.de>, Wed, 27 Jun 2012 23:55:25, Bjoern Hoehrmann
>><bjoern@hoehrmann.de> posted:
>>
>>>* phil hunt wrote in comp.lang.javascript:
>>>>In Jsctipt, I have
>>>>
>>>>re = new RegExp("one|two", "gi");
>>>>
>>>>"one three".search(re);  return an integer >-1, meaning it found a match
>>>>
>>>>My question is how do I set up the RegExp to match "one" AND "two". I 
>>>>though
>>>>an & would do it, but no.
>>>
>>>JavaScript regular expressions do not support intersections, you would
>>>have to use several regular expressions (or compute the intersection in
>>>some other tool).
>>
>> Not necessarily.    re = new RegExp("one.*two|two.*one", "gi");
>>
>> But should "oneight" be considered to contain both "one" & "eight" ?
>>
>
> How about this? I am assuming these are disjoint strings
> (as someone noted - do you want to see a match of "one" and
> "eleven" in the string "oneleven"?) or at least can only appear
> disjointly.
>
> Do a global match on the alternation/disjunction of the
> strings, 'one|two" and see how many (distinct) matches
> you get
>   str.match(/one|two/g).length
> will give the number of matches (if it's two, you matched
> two times - but maybe you matched "one" twice!).
>
> If you want to count the number of distinct matches ... well,
> it is easier to count the number of distinct elements of an
> array if it is sorted, so ...
>
> LOOKING FOR "one" and/or "two" IN A STRING, str:
> ------------------------------------------------
>   str="onetwothreetwoone";
>
>   old="";count=0;
>   str.match(/one|two/g).sort().forEach(function f(x){(x != old) && 
> count++; old=x});
>   // count is the result - test it.
>
> "count" is the number of different matches.
> If it is two you matched both. 

[toc] | [prev] | [next] | [standalone]


#14766

FromSpamless <Spamless@Nil.nil>
Date2012-06-29 11:36 -0500
Message-ID<-OydnWwzV_IYRHDSnZ2dnUVZ_oSdnZ2d@inch.com>
In reply to#14763
On 2012-06-29, Phil Hunt <aaa@aaa.com> wrote:
> My requirement deos not care how many times or where the string may occur.

Well, if you want to find out if both "one" and "two" are in the string
and you can find the number of matches of different content to 'one|two'
(different content - not the same content at different locations) then you
do want to know that number - or at least that it is two (if it's two you
matched both, otherwise you matched one or none.)

> I have another question. When the patern contains '(' it get an exception.
> How do I escape that and other character that may give me trouble.

I remember trying to do a string.replace(/./g,...) on a string with embedded
end of line characters and /./ does not match those. So ... I use /\x00-\xff/
(they were all single byte characters). That doesn't help here, though,
since a /...(.../ will take "(" as marking the beginning of a group. In that
case you need to escape "(" (and ")"). That is a little tricky.

  re = /\)/;
  ")".match(re)
matches but ...

  string="\)";
  re=/string/;
  ")".match(re)
does not match.

Why? This is literal syntax and re=/string/ matches
the string "string".

  re = RegExp(")");
tries to create a regular expression with a group closer
(")") and fails.

  re = RegExp("\)") also fails (the string "\)" is, internally,
                                 still the string ")")

When you set string="\)" the internally stored string
is the escaped value of ")". In Javascript everything
has an escaped value and if a separate/different value
is not specified, then the escaped value is the same as
the normal value (this is different from, say, the way
PHP handles escapes). In Javascript, "\Q" is (internally)
just (the same as) "Q". If Q is not escapable in PHP then
"\Q" is (internally) "\Q" (it does not lose the backslash).
If you really want "\Q" in a string, you can use "\\Q"
or even (since, in Javascript \Q and Q are the same),
"\\\Q"!

We want "\)" internally (the string to be used in the
regex) so we use "\\)" (or even "\\\)"!).

  string="\\)";
  re=RegExp(string);
  ")".match(re)

(even string="\\\)";
      re=RegExp(string);
 works)
  matches.

Here, '\\)' is internally '\)' (so is '\\\)')
while "\)" is just ")" internally.

[toc] | [prev] | [next] | [standalone]


#14767

FromGene Wirchenko <genew@ocis.net>
Date2012-06-29 10:05 -0700
Message-ID<62oru7to390oitbpqn6q3m68oegmf25h9q@4ax.com>
In reply to#14763
On Fri, 29 Jun 2012 10:19:47 -0400, "Phil Hunt" <aaa@aaa.com> wrote:

>My requirement deos not care how many times or where the string may occurs.
>
>I have another question. When the patern contains '(' it get an exception.
>How do I escape that and other character that may give me trouble.

     Escape by prefixing the character with a backslash.

[snip]

Sincerely,

Gene Wirchenko

[toc] | [prev] | [next] | [standalone]


#14736

FromGene Wirchenko <genew@ocis.net>
Date2012-06-27 19:38 -0700
Message-ID<q1gnu759m1k9lbf8jdqqcam93edn4csn82@4ax.com>
In reply to#14726
On Wed, 27 Jun 2012 17:48:15 -0400, "phil hunt" <a@abc.com> wrote:

>In Jsctipt, I have
>
>re = new RegExp("one|two", "gi");
>
>"one three".search(re);  return an integer >-1, meaning it found a match
>
>My question is how do I set up the RegExp to match "one" AND "two". I though 
>an & would do it, but no.

     If you mean that the string contains both "one" and "two", the
regex would be
          one.*two|two.*one
Warning: "." does not match newline.  It does match any other
character.

     This approach is not workable if you have many terms as the
complexity of the expression is O(n!).  For three terms, there would
be six alternatives.  For four, 24.

Sincerely,

Gene Wirchenko

[toc] | [prev] | [next] | [standalone]


#14737

FromGene Wirchenko <genew@ocis.net>
Date2012-06-27 21:18 -0700
Message-ID<8imnu71dci767guonvmnveql8sgn6p84pe@4ax.com>
In reply to#14736
On Wed, 27 Jun 2012 19:38:34 -0700, Gene Wirchenko <genew@ocis.net>
wrote:

>On Wed, 27 Jun 2012 17:48:15 -0400, "phil hunt" <a@abc.com> wrote:
>
>>In Jsctipt, I have
>>
>>re = new RegExp("one|two", "gi");
>>
>>"one three".search(re);  return an integer >-1, meaning it found a match
>>
>>My question is how do I set up the RegExp to match "one" AND "two". I though 
>>an & would do it, but no.
>
>     If you mean that the string contains both "one" and "two", the
>regex would be
>          one.*two|two.*one
>Warning: "." does not match newline.  It does match any other
>character.
>
>     This approach is not workable if you have many terms as the
>complexity of the expression is O(n!).  For three terms, there would
>be six alternatives.  For four, 24.

     Oops!  I just realised an implicit assumption in my solution,
namely that terms are independent.  If they overlap, my solution would
not work properly.  For example, if the terms are "one" and "eleven",
"oneleven" contains both terms, but my method will not detect this
case.

     If this is an issue, then just do two string searches.  If you
are just searching for ordinary strings, you might be better off not
using regexes.

Sincerely,

Gene Wirchenko

[toc] | [prev] | [next] | [standalone]


#14738

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2012-06-28 06:24 +0000
Message-ID<XnsA080557FD310Deejj99@194.109.133.133>
In reply to#14736
Gene Wirchenko wrote on 28 jun 2012 in comp.lang.javascript:

> On Wed, 27 Jun 2012 17:48:15 -0400, "phil hunt" <a@abc.com> wrote:
> 
>>In Jsctipt, I have
>>
>>re = new RegExp("one|two", "gi");
>>
>>"one three".search(re);  return an integer >-1, meaning it found a
>>match 
>>
>>My question is how do I set up the RegExp to match "one" AND "two". I
>>though an & would do it, but no.
> 
>      If you mean that the string contains both "one" and "two", the
> regex would be
>           one.*two|two.*one

That's the idea!

Whatsay, John?

> Warning: "." does not match newline.  It does match any other
> character.

I suggest when seperate words are ment,
and give a multiline alternative:

================================

function myTest(text) {
  r = text.replace(/\n/,'[newline]') + ' -> '  
  r += /\bone\b.*\btwo\b|\btwo\b.*\bone\b/.test(text);
  r += '<br>'
  document.write( r ); 
};

myTest('two one');
myTest('one two');
myTest('onetwo');
myTest('two ones');
myTest('two \n one');

function myTestMulti(text) {
  r = text.replace(/\n/,'[newline]') + ' -> '  
  r += /\bone\b[\s\S]*\btwo\b|\btwo\b[\s\S]*\bone\b/.test(text);
  r += '<br>'
 document.write( r ); 
};

myTestMulti('two \n one');

=================================

two one -> true
one two -> true
onetwo -> false
two ones -> false
two [newline] one -> false
two [newline] one -> true

-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#14743

FromSpamless <Spamless@Nil.nil>
Date2012-06-28 05:36 -0500
Message-ID<9fudnXyX9bFYrnHSnZ2dnUVZ_qGdnZ2d@inch.com>
In reply to#14738
On 2012-06-28, Evertjan. <exxjxw.hannivoort@inter.nl.net> wrote:
> Gene Wirchenko wrote on 28 jun 2012 in comp.lang.javascript:
>
>> On Wed, 27 Jun 2012 17:48:15 -0400, "phil hunt" <a@abc.com> wrote:
>> 
>>>In Jsctipt, I have
>>>
>>>re = new RegExp("one|two", "gi");
>>>
>>>"one three".search(re);  return an integer >-1, meaning it found a
>>>match 
>>>
>>>My question is how do I set up the RegExp to match "one" AND "two". I
>>>though an & would do it, but no.
>> 
>>      If you mean that the string contains both "one" and "two", the

Wouldn't it be nice if you knew the order of the words?
Sometimes I ... split the text on word boundaries to get an array,
sort that (now the words are in order) join those up with a space
separator (now you have a new string with the same words in order)
and now you just search for "one.*two" (or with word boundaries
if you don't want subsequences of longer words).

[toc] | [prev] | [next] | [standalone]


#14751

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2012-06-28 15:15 +0000
Message-ID<XnsA080AF8ECFF79eejj99@194.109.133.133>
In reply to#14743
Spamless wrote on 28 jun 2012 in comp.lang.javascript:

> On 2012-06-28, Evertjan. <exxjxw.hannivoort@inter.nl.net> wrote:
>> Gene Wirchenko wrote on 28 jun 2012 in comp.lang.javascript:
>>
>>> On Wed, 27 Jun 2012 17:48:15 -0400, "phil hunt" <a@abc.com> wrote:
>>> 
>>>>In Jsctipt, I have
>>>>
>>>>re = new RegExp("one|two", "gi");
>>>>
>>>>"one three".search(re);  return an integer >-1, meaning it found a
>>>>match 
>>>>
>>>>My question is how do I set up the RegExp to match "one" AND "two". I
>>>>though an & would do it, but no.
>>> 
>>>      If you mean that the string contains both "one" and "two", the
> 
> Wouldn't it be nice if you knew the order of the words?
> Sometimes I ... split the text on word boundaries to get an array,
> sort that (now the words are in order) join those up with a space
> separator (now you have a new string with the same words in order)
> and now you just search for "one.*two" (or with word boundaries
> if you don't want subsequences of longer words).

Wouldn't it be nice if you quoted me including something I wrote?

-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#14752

From"Phil Hunt" <aaa@aaa.com>
Date2012-06-28 12:08 -0400
Message-ID<jsi02m$h27$1@speranza.aioe.org>
In reply to#14751
Thank you guys for all your input.
I end up looping thru all the words(actually I am not looking for words, I 
am searching an array of string as someone is typing).

I am not that eager to delve into the crazy synax of RE, so looping is 
easier. I suppose I can use string.contains ?.

I am using MS visual studio to do some web pages. JScript is all I use, but 
it provides me with very little intellsense, What's up with that. What tools 
do you guys use that give you hints ? I hate to google or bother you guys 
with stupid questions



"Evertjan." <exxjxw.hannivoort@inter.nl.net> wrote in message 
news:XnsA080AF8ECFF79eejj99@194.109.133.133...
> Spamless wrote on 28 jun 2012 in comp.lang.javascript:
>
>> On 2012-06-28, Evertjan. <exxjxw.hannivoort@inter.nl.net> wrote:
>>> Gene Wirchenko wrote on 28 jun 2012 in comp.lang.javascript:
>>>
>>>> On Wed, 27 Jun 2012 17:48:15 -0400, "phil hunt" <a@abc.com> wrote:
>>>>
>>>>>In Jsctipt, I have
>>>>>
>>>>>re = new RegExp("one|two", "gi");
>>>>>
>>>>>"one three".search(re);  return an integer >-1, meaning it found a
>>>>>match
>>>>>
>>>>>My question is how do I set up the RegExp to match "one" AND "two". I
>>>>>though an & would do it, but no.
>>>>
>>>>      If you mean that the string contains both "one" and "two", the
>>
>> Wouldn't it be nice if you knew the order of the words?
>> Sometimes I ... split the text on word boundaries to get an array,
>> sort that (now the words are in order) join those up with a space
>> separator (now you have a new string with the same words in order)
>> and now you just search for "one.*two" (or with word boundaries
>> if you don't want subsequences of longer words).
>
> Wouldn't it be nice if you quoted me including something I wrote?
>
> -- 
> Evertjan.
> The Netherlands.
> (Please change the x'es to dots in my emailaddress) 

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web