473,407 Members | 2,312 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,407 software developers and data experts.

Regular Expression Help

Hi all,

I'm going down the road of learing the pattern matching in regular
expressions, and I'm trying to convert the characters into English in my
head so I can see whats happening...

For clarity with this one...

\[b\]([^\]]+)\[\/b\]

Ok - the first thing I did was break it up - because it looks a nightmare
like that...so now I have..3 parts..

\[b\]

([^\]]+)

\[\/b\]

In part 1 I under stand that the \ is telling the expression that a special
character is coming next, thus escaping the [ and ] respectively, thus ,
and in part 3 I understand the same and also the escaping of the /, thus
- no problems so far...

In part 2 I'm assuming that the brackets are seperating a "pattern" so that
I might reference it as $1 later on, I understand that the [^] are saying
"not enclosed" so therefore it's going to ignore a ], the + sign however
perplexes me - my VBScript book says "Matches the preceeding character one
or more times" - so does that mean the ] just before it, or does it mean the
character OR characters defined within the [ ] etc?

Any help would be appreciated - thus far I've managed to 'wing' my way
through what I've needed to do - but it's getting more complicated now :)

Thanks in advance

Rob
Jul 22 '05 #1
3 1238

"Rob Meade" <ro********@NO-SPAM.kingswoodweb.net> wrote in message
news:Ft******************@text.news.blueyonder.co. uk...
: Hi all,
:
: I'm going down the road of learing the pattern matching in regular
: expressions, and I'm trying to convert the characters into English in my
: head so I can see whats happening...
:
: For clarity with this one...
:
: \[b\]([^\]]+)\[\/b\]
:
: Ok - the first thing I did was break it up - because it looks a nightmare
: like that...so now I have..3 parts..
:
: \[b\]
:
: ([^\]]+)
:
: \[\/b\]
:
: In part 1 I under stand that the \ is telling the expression that a
special
: character is coming next, thus escaping the [ and ] respectively, thus
,
: and in part 3 I understand the same and also the escaping of the /, thus
:
- no problems so far...
:
: In part 2 I'm assuming that the brackets are seperating a "pattern" so
that
: I might reference it as $1 later on, I understand that the [^] are saying
: "not enclosed" so therefore it's going to ignore a ], the + sign however
: perplexes me - my VBScript book says "Matches the preceeding character one
: or more times" - so does that mean the ] just before it, or does it mean
the
: character OR characters defined within the [ ] etc?

the character OR characters defined within the [ ] etc?
[ ] as a negative character set. Matches any character not ]
\] as a literal ]
+ one or more characters in the character set

It says it doesn't want ] in between

This looks like forum code where forums, portals, etc. don't allow HTML
coding but do allow forum coding and this it to capture the content in the
( ) group and replace the forum code with <b></b> to make it bold.
They could also use an inline style with: <span style="font-weight:
bold"></span>.

--
Roland Hall
/* This information is distributed in the hope that it will be useful, but
without any warranty; without even the implied warranty of merchantability
or fitness for a particular purpose. */
Technet Script Center - http://www.microsoft.com/technet/scriptcenter/
WSH 5.6 Documentation - http://msdn.microsoft.com/downloads/list/webdev.asp
MSDN Library - http://msdn.microsoft.com/library/default.asp
Jul 22 '05 #2
"Rob Meade" <ro********@NO-SPAM.kingswoodweb.net> wrote in message
news:Ft******************@text.news.blueyonder.co. uk...

\[b\]([^\]]+)\[\/b\]


I think it might be easier if you search for a general tag
then find its matching closing tag
then recursively process the string between the two.

e.g.

\[(\w+)=?([^\]]*)\]

which is...

[ followed by
a word - \w+ is the same as [A-Za-z0-9_]+
followed by one (optional) equals sign
possibly followed by some other stuff before the ]

For instance...

function fnparse(fString)
dim re, ms, m
dim token, closetag
dim iStart, iEnd
dim s, t

set re = new regexp
re.pattern = "\[(\w+)=?([^\]]*)\]"
set ms = re.execute(fString)
If ms.Count > 0 Then
set m = ms(0)
iStart = m.FirstIndex + m.Length + 1
token = m.SubMatches(0) 'b, i, url etc.
t = m.SubMatches(1) 'tag attribute (if any)
closetag = "[/" & token & "]"
iEnd = InStr(iStart, fString, closetag, 1)
if iEnd > 0 Then
s = Mid(fString, iStart, iEnd - iStart)
else 'error: closing tag not found
s = Mid(fString, iStart)
end if
select case LCase(token)
case "url"
if len(t) > 0 then t = " href='" & t & "'"
s = "<a" & t & ">" & fnparse(s) & "</a>"
'case (any other special cases here)
case else
s = "<" & token & ">" & fnparse(s) & "</" & token & ">"
end select
'output any string to the left of the tag
'then the processed string
'then process the rest of the input
iEnd = iEnd + len(closetag)
s = Mid(fString, 1, m.FirstIndex) & s & fnparse(Mid(fString, iEnd))
else 'no tags found
s = fString
end if
fnparse = s

end function
The function uses 'SubMatches' which
is not in earlier versions of VBScript

--
roger
Jul 22 '05 #3
oops... I meant to reply to the 'bbcode parsing' thread
but clicked on the wrong post

--
roger
Jul 22 '05 #4

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

Similar topics

5
by: Bradley Plett | last post by:
I'm hopeless at regular expressions (I just don't use them often enough to gain/maintain knowledge), but I need one now and am looking for help. I need to parse through a document to find a URL,...
4
by: Neri | last post by:
Some document processing program I write has to deal with documents that have headers and footers that are unnecessary for the main processing part. Therefore, I'm using a regular expression to go...
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...
9
by: Pete Davis | last post by:
I'm using regular expressions to extract some data and some links from some web pages. I download the page and then I want to get a list of certain links. For building regular expressions, I use...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...

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.