473,394 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Regular Expression pattern

Greeting,

I want to extract all "[XXX]" from a string, what pattern should I
used?

eg.
[test1] = [test2]

- return array [test1] or test1, [test2] or test2

I tried \[(.*)\] , but it returned the whole "[test1] = [test2]"!

Anyone can help? Also, got any good reference sites especially with
rich example for this patterning?
Jul 20 '05 #1
5 3306

"Kelmen Wong" <ke****@hotmail.com> schreef in bericht
news:84**************************@posting.google.c om...
Greeting,

I want to extract all "[XXX]" from a string, what pattern should I
used?


var string = "[test1] = [test2]";
var reg = /\[[^\]]+\]/g;
var matches = string.match(reg);

for (m in matches) alert(matches[m]);
JW

Jul 20 '05 #2
ke****@hotmail.com (Kelmen Wong) writes:
I want to extract all "[XXX]" from a string, what pattern should I
used?
You want to have a string, and then create an array of all strings in
it that are delimitered by "[" and "]"?
eg.
[test1] = [test2]

- return array [test1] or test1, [test2] or test2

I tried \[(.*)\] , but it returned the whole "[test1] = [test2]"!


Yes, regular expressions try to match as much as possible.
The trick is to not use "." between the two brackets, but [^\]], i.e.,
matchin everything except an end bracket.

Try
function extractBracketed(str) {
var result = [];
var re = /\[([^\]]*)\]/g ;
var match;
while ((match = re.exec(str))) {
result.push(match[1]);
}
return result;
}

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #3
ke****@hotmail.com (Kelmen Wong) wrote in message news:<84**************************@posting.google. com>...

<html>

<head>
<title>none-greedy RegExp-pattern</title>
<script type="text/javascript">
<!--
/*
hi Kelmen,

eg.
[test1] = [test2]

- return array [test1] or test1, [test2] or test2

I tried \[(.*)\] , but it returned the whole "[test1] = [test2]"!


thats due to the greedy nature of RegExp-patterns;

and "greedy" means:
a pattern alway tries to take as much chracters of
a given string as long as the search rule will match;

in your example "[test1] = [test2]" the last possible
character that will fit with the RegExp /\[(.*)\]/;
is the closing square-bracket of "...test2]" because
of ".*" that allowes as much characters as possible
between an opening and a closing square-bracket;

none-greedy patterns will solve your problem;

you might play with the following lines:
*/

var givenString = "test1 [test2] test3] [test4] [test5] [test6
[test7 test8]";
var regExpression = /\[[^\]]*\]/g;
// look for a opening square bracket - \[ -
// then for everything that is not a closing square bracket -
[^\]] - as often as you can - * -
// till you find a closing square bracket - \] -
// and use this pattern for the entire string - g -
var matches = [];

if (regExpression.test(givenString)) {
matches = givenString.match(regExpression);
}
alert("matches.length = " + matches.length +
"\nmatches.join(\"\\n\") :\n" + matches.join("\n"));
regExpression = /\[[^\[\]]*\]/g;
// look for a opening square bracket - \[ -
// then for everything that is neither a opening nor a closing
square bracket - [^\[\]] - as often as you can - * -
// till you find a closing square bracket - \] -
// and use this pattern for the entire string - g -
if (regExpression.test(givenString)) {
matches = givenString.match(regExpression);
}
alert("matches.length = " + matches.length +
"\nmatches.join(\"\\n\") :\n" + matches.join("\n"));

givenString = "[test1] = [test2]";
if (regExpression.test(givenString)) {
matches = givenString.match(regExpression);
}
alert("matches.length = " + matches.length +
"\nmatches.join(\"\\n\") :\n" + matches.join("\n"));

// if you like to free your matches from its enclosing brackets
treat your "matches"-array as shown right now:
//
// matches = matches.join("splitnjoin");alert(matches);
// matches = matches.replace(/\[/g,"");alert(matches);
// matches = matches.replace(/\]/g,"");alert(matches);
// matches = matches.split("splitnjoin");alert(matches);
//
matches = matches.join("splitnjoin").replace(/\[/g,"").replace(/\]/g,"").split("splitnjoin");

alert("free from enclosing brackets\n\nmatches.join(\"\\n\") :\n"
+ matches.join("\n"));

// NOTE:
//
// in javascript 1.5 none-greedy search patterns can easly
// be written by using the punctuation character "?";
//
// /\[[^\]]*\]/g; then will be /\[.*?\]/g;

/*
givenString = "test1 [test2] test3] [test4] [test5] [test6 [test7
test8]";
regExpression = /\[.*?\]/g; // was: /\[[^\]]*\]/g;

if (regExpression.test(givenString)) {
matches = givenString.match(regExpression);
}
alert("matches.length = " + matches.length +
"\nmatches.join(\"\\n\") :\n" + matches.join("\n"));
// /\[[^\[\]]*\]/g; will turn into /\[[^\[]*?\]/g;

regExpression = /\[[^\[]*?\]/g; // was: /\[[^\[\]]*\]/g;

if (regExpression.test(givenString)) {
matches = givenString.match(regExpression);
}
alert("matches.length = " + matches.length +
"\nmatches.join(\"\\n\") :\n" + matches.join("\n"));
*/

//
// have fun - regards - peterS. - ps******@gmx.net
//

//-->
</script>
<head>

<body>
</body>

</html>
Jul 20 '05 #4
ps******@gmx.net (peter seliger) writes:
thats due to the greedy nature of RegExp-patterns;

and "greedy" means:
a pattern alway tries to take as much chracters of
a given string as long as the search rule will match;


In modern implementations of Javscrip (i.e., ECMAScript) you can
use a non-greedy * or + operator (like you can in Perl).

The regular expression is then:

/\[.*?\]/

It finds a "[" followed by the shortest possible match of any characters,
followed by a "]".

It doesn't work in, e.g., Netscape 4.
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #5
ke****@hotmail.com (Kelmen Wong) wrote in message news:<84**************************@posting.google. com>...
<html>

<head>
<title>none-greedy RegExp-pattern</title>
<script type="text/javascript">
<!--
/*
hi Kelmen,

eg.
[test1] = [test2]

- return array [test1] or test1, [test2] or test2

I tried \[(.*)\] , but it returned the whole "[test1] = [test2]"!


thats due to the greedy nature of RegExp-patterns;

and "greedy" means:
a pattern alway tries to take as much chracters of
a given string as long as the search rule will match;

in your example "[test1] = [test2]" the last possible
character that will fit with the RegExp /\[(.*)\]/;
is the closing square-bracket of "...test2]" because
of ".*" that allowes as much characters as possible
between an opening and a closing square-bracket;

none-greedy patterns will solve your problem;

you might play with the following lines:
*/

var givenString = "test1 [test2] test3] [test4] [test5] [test6
[test7 test8]";
var regExpression = /\[[^\]]*\]/g;
// look for a opening square bracket - \[ -
// then for everything that is not a closing square bracket -
[^\]] - as often as you can - * -
// till you find a closing square bracket - \] -
// and use this pattern for the entire string - g -
var matches = [];

if (regExpression.test(givenString)) {
matches = givenString.match(regExpression);
}
alert("matches.length = " + matches.length +
"\nmatches.join(\"\\n\") :\n" + matches.join("\n"));
regExpression = /\[[^\[\]]*\]/g;
// look for a opening square bracket - \[ -
// then for everything that is neither a opening nor a closing
square bracket - [^\[\]] - as often as you can - * -
// till you find a closing square bracket - \] -
// and use this pattern for the entire string - g -
if (regExpression.test(givenString)) {
matches = givenString.match(regExpression);
}
alert("matches.length = " + matches.length +
"\nmatches.join(\"\\n\") :\n" + matches.join("\n"));

givenString = "[test1] = [test2]";
if (regExpression.test(givenString)) {
matches = givenString.match(regExpression);
}
alert("matches.length = " + matches.length +
"\nmatches.join(\"\\n\") :\n" + matches.join("\n"));

// if you like to free your matches from its enclosing brackets
treat your "matches"-array as shown right now:
//
// matches = matches.join("splitnjoin");alert(matches);
// matches = matches.replace(/\[/g,"");alert(matches);
// matches = matches.replace(/\]/g,"");alert(matches);
// matches = matches.split("splitnjoin");alert(matches);
//
matches = matches.join("splitnjoin").replace(/\[/g,"").replace(/\]/g,"").split("splitnjoin");

alert("free from enclosing brackets\n\nmatches.join(\"\\n\") :\n"
+ matches.join("\n"));

// NOTE:
//
// in javascript 1.5 none-greedy search patterns can easly
// be written by using the punctuation character "?";
//
// /\[[^\]]*\]/g; then will be /\[.*?\]/g;

/*
givenString = "test1 [test2] test3] [test4] [test5] [test6 [test7
test8]";
regExpression = /\[.*?\]/g; // was: /\[[^\]]*\]/g;

if (regExpression.test(givenString)) {
matches = givenString.match(regExpression);
}
alert("matches.length = " + matches.length +
"\nmatches.join(\"\\n\") :\n" + matches.join("\n"));
// /\[[^\[\]]*\]/g; will turn into /\[[^\[]*?\]/g;

regExpression = /\[[^\[]*?\]/g; // was: /\[[^\[\]]*\]/g;

if (regExpression.test(givenString)) {
matches = givenString.match(regExpression);
}
alert("matches.length = " + matches.length +
"\nmatches.join(\"\\n\") :\n" + matches.join("\n"));
*/

//
// have fun - regards - peterS. - ps******@gmx.net
//

//-->
</script>
<head>

<body>
</body>

</html>
Jul 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
1
by: Jeff | last post by:
For the life of me, I can't create a working regular expression for a schema pattern to do the following: Validates if an entire word does NOT match the entire word in the pattern. For...
3
by: rodchar | last post by:
hey all, what would my expression look like if i wanted to make sure that the input matched the following pattern. c:\filename.ext it doesn't have to be the c drive just a letter, colon,...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
25
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
3
by: shapper | last post by:
Hello, I have a regular expression to validate email addresses: "\w+(\w+)*@\w+(\w+)*\.\w+(\w+)*" Now I need to force all emails to be from a given domain, for example, accept only:...
6
by: rorymo | last post by:
I have a regular expression that allows only certain characters to be valid in an xml doc as follows: <xs:pattern value="^*" /> What I want to do is also allow any unicode character that is...
5
by: shawnmkramer | last post by:
Anyone every heard of the Regex.IsMatch and Regex.Match methods just hanging and eventually getting a message "Requested Service not found"? I have the following pattern: ^(?<OrgCity>(+)+),...
0
by: altavim | last post by:
Usually when you make regular expression to extract text you are starting from simple expression. When you got to know target text, you are extending your expression. Subsequently very hard to ready...
7
by: blaine | last post by:
Hey everyone, For the regular expression gurus... I'm trying to write a string matching algorithm for genomic sequences. I'm pulling out Genes from a large genomic pattern, with certain start...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.