473,603 Members | 2,642 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

RegExp doesn't capture matches in parentheses?

Has anyone ever encountered trouble with regular expressions not
capturing matches specified by parentheses? I seem to have a weird
situation where a regular expression is matching the String I'm running
it against, but none of the parenthized expressions are coming back in
the array.

I'm basically trying to pull a topic out of a bulletin board page. So
I'll run:

var bodystr = new
String(document .getElementsByT agName('body')[0].innerHTML)
;
var topicPattern = /<p style="margin-left: 4px;"><b><font
face="Arial" size=
"2">(.*?)<\/font><\/b><\/p>/ig;
var matches = bodystr.match(t opicPattern);

And here's the weird thing. Apparently, matches[0] is filled, with the
string that matches the whole pattern....

But there's nothing in matches[1]. Also nothing in RegExp.$1.

Am I misunderstandin g how this is supposed to work, or is there
something genuinely odd about this?

If anyone wants to see this in context, here's an example:

http://mmedia.csoft.net/LE/testpage.html

Click the link in the upperhand corner to see it in action. Feel free
to poke around that directory, too.

Jul 23 '05 #1
1 2131
On 28/06/2005 09:48, weston wrote:
Has anyone ever encountered trouble with regular expressions not
capturing matches specified by parentheses?
No. :)

[snip]
var bodystr = new
String(document .getElementsByT agName('body')[0].innerHTML)
;
Creating a String object isn't really necessary unless you are going to
call multiple methods on that object.
var topicPattern = /<p style="margin-left: 4px;"><b><font
face="Arial" size="2">(.*?)< \/font><\/b><\/p>/ig;
I don't really recommend using non-greedy quantifiers as some older
browsers will think them syntax errors. I'd use

([^<]*)

as you're just trying to avoid matching the next tag.
var matches = bodystr.match(t opicPattern);

And here's the weird thing. Apparently, matches[0] is filled, with the
string that matches the whole pattern....

But there's nothing in matches[1]. Also nothing in RegExp.$1.
All is as it should be. When the match method is called with a regular
expression that has its global flag set, it calls the exec method of
that regular expression until no more matches can be found, and returns
an array containing the first element (the matching sub-string) from
each of the exec method results.

Call the exec method instead:

match = topicPattern.ex ec(bodystr);

The second element (matches[1]) will contain the captured value.

[snip]
http://mmedia.csoft.net/LE/testpage.html

Click the link in the upperhand corner to see it in action. Feel free
to poke around that directory, too.


All I find is a lot of script errors, and not much more.

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #2

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

Similar topics

4
5483
by: | last post by:
Hi, I'm fairly new to regular expressions, and this may be a rather dumb question, but so far I haven't found the answer in any tutorial or reference yet... If I have f.i. the string "The {{{{power of {{{{regular expressions}}}} comes from}}}} the ability to include alternatives and repetitions in the pattern." from which I want to extract chunks starting with "{{{{" and ending with "}}}}".
3
2124
by: Boris Pelakh | last post by:
I'm trying to extract name:value pairs from a string similar to this one {a:b,c:d,e:f} The individual pieces are more complicated, but its similar in principal Here is a test script I use $f = "a:b,c:d,e:f"; @a = ($f =~ m/(?:(\w+):(\w+))(?:,(?:(\w+):(\w+)))+/);
3
9164
by: Jane Doe | last post by:
Hello, I need to browse a list of hyperlinks, each followed by an author, and remove the links only for certain authors. 1. I searched the archives on Google, but didn't find how to tell the RegExp object to be non-greedy as using the ? quantifier doesn't seem to work. --------- SAMPLE ----------------
4
1600
by: Chris Beach | last post by:
Hi there, I'm attempting to extract a number from a string, and I'm using the following code: Regex prRegexp = new Regex( @"Rank_1:\d+:(\d+)" ); MatchCollection matches = prRegexp.Matches( responseText.ToString() );
8
2016
by: Dmitry Korolyov | last post by:
ASP.NET app using c# and framework version 1.1.4322.573 on a IIS 6.0 web server. A single-line asp:textbox control and regexp validator attached to it. ^\d+$ expression does match an empty string (when you don't enter any values) - this is wrong d+ expression does not match, for example "g24" string - this is also wrong www.regexplib.com test validator works fine for both cases, i.e. it is reporting "not match" for the...
2
1116
by: Eric Gibson | last post by:
Hey all, I'm trying to do a regexp that will gracefully extract the fields (fn) from a string in this format: (f1, f2, f3),(f1, f2, f3),(f1, f2, f3),...,(f1, f2, f3) Is there a way I could grab each (f1, f2, f3) and then iterate through them gracefully. I'm using Split(str, ",\)") , but then I have to remove the ('s and )'s from the beginning and end of the first and last () set.
7
3435
by: Csaba Gabor | last post by:
I need to come up with a function function regExpPos (text, re, parenNum) { ... } that will return the position within text of RegExp.$parenNum if there is a match, and -1 otherwise. For example: var re = /some(thing|or other)?.*(n(est)(?:ed)?.*(parens) )/ var text = "There were some nesting parens in the test"; alert (regExpPos (text, re, 3));
4
1197
by: eight02645999 | last post by:
hi suppose i have a string like test1?test2t-test3*test4*test5$test6#test7*test8 how can i construct the regexp to get test3*test4*test5 and test7*test8, ie, i want to match * and the words before and after? thanks
27
3232
by: SQL Learner | last post by:
Hi all, I have an Access db with two large tables - 3,100,000 (tblA) and 7,000 (tblB) records. I created a select query using Inner Join by partial matching two fields (X from tblA and Y from tblB). The size of the db is about 200MBs. Now my issue is, the query has been running for over 3 hours already - I have no idea when it will end. I am using Access 2003. Are there ways to improve the speed performance? (Also, would the...
0
7929
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8419
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8406
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8274
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6737
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5882
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5441
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3906
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2433
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.