473,836 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

regalar expression match

New to Regex and I'm having a hard time figuring this one out.

I need a regular expression what will based on balanced square brackets.
For example:
[$AA[123]], [BB[bb],CC], [a[b[c]]]

I'm trying to write a reg ex that will parse the above into 3 pieces:
1) [$AA[123]] (or $AA[123] would be fine)
2) [BB[bb],CC]
3) [a[b[c]]]

Basically, any time the square brackets balance, I want to be able to pluck
out that value.

I wrote: (?<column>\[([^\[\]]*(\[.*\])*)*\])

Basically, look for a [, then repeatly take either a non-"[" and non-"]"
character or find an internal set of [...] and take everything between them;
until you hit a ]. It's the later part that doesn't work as I can't seem to
figure out how to get the brackets to balance properly. Is this just
something regular expression are not meant to do? Thanks for any help.
Nov 15 '05 #1
12 1845
I think String.split(", ") help. Since you have comma as delimiter
eg. code
String s = "[a][b][c[d]], [a[b]], c[d][e]";
String sa[] = s.Split(",");

this will give me an array, which I can check length of & traverse using a for loop to check the contents of other elements

:-)
Kalpesh

Jed Ozone wrote:
New to Regex and I'm having a hard time figuring this one out.

I need a regular expression what will based on balanced square brackets.
For example:
[$AA[123]], [BB[bb],CC], [a[b[c]]]

I'm trying to write a reg ex that will parse the above into 3 pieces:
1) [$AA[123]] (or $AA[123] would be fine)
2) [BB[bb],CC]
3) [a[b[c]]]


Nov 15 '05 #2
Hi Jed,

Why don't you create a state machine rather - iterate through string and
count [ (+1) and ] (-1) - when the count=0 and you are over a comma, you
will know that the comma is delimiter.

--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com

"Jed Ozone" <jed oz***@yahoo.com> wrote in message
news:OY******** ******@TK2MSFTN GP11.phx.gbl...
New to Regex and I'm having a hard time figuring this one out.

I need a regular expression what will based on balanced square brackets.
For example:
[$AA[123]], [BB[bb],CC], [a[b[c]]]

I'm trying to write a reg ex that will parse the above into 3 pieces:
1) [$AA[123]] (or $AA[123] would be fine)
2) [BB[bb],CC]
3) [a[b[c]]]

Basically, any time the square brackets balance, I want to be able to pluck out that value.

I wrote: (?<column>\[([^\[\]]*(\[.*\])*)*\])

Basically, look for a [, then repeatly take either a non-"[" and non-"]"
character or find an internal set of [...] and take everything between them; until you hit a ]. It's the later part that doesn't work as I can't seem to figure out how to get the brackets to balance properly. Is this just
something regular expression are not meant to do? Thanks for any help.

Nov 15 '05 #3
I think String.split(", ") help. Since you have comma as delimiter
eg. code
String s = "[a][b][c[d]], [a[b]], c[d][e]";
String sa[] = s.Split(",");

this will give me an array, which I can check length of & traverse using a for loop to check the contents of other elements

:-)
Kalpesh

Jed Ozone wrote:
New to Regex and I'm having a hard time figuring this one out.

I need a regular expression what will based on balanced square brackets.
For example:
[$AA[123]], [BB[bb],CC], [a[b[c]]]

I'm trying to write a reg ex that will parse the above into 3 pieces:
1) [$AA[123]] (or $AA[123] would be fine)
2) [BB[bb],CC]
3) [a[b[c]]]


Nov 15 '05 #4
Hi Jed,

Why don't you create a state machine rather - iterate through string and
count [ (+1) and ] (-1) - when the count=0 and you are over a comma, you
will know that the comma is delimiter.

--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com

"Jed Ozone" <jed oz***@yahoo.com> wrote in message
news:OY******** ******@TK2MSFTN GP11.phx.gbl...
New to Regex and I'm having a hard time figuring this one out.

I need a regular expression what will based on balanced square brackets.
For example:
[$AA[123]], [BB[bb],CC], [a[b[c]]]

I'm trying to write a reg ex that will parse the above into 3 pieces:
1) [$AA[123]] (or $AA[123] would be fine)
2) [BB[bb],CC]
3) [a[b[c]]]

Basically, any time the square brackets balance, I want to be able to pluck out that value.

I wrote: (?<column>\[([^\[\]]*(\[.*\])*)*\])

Basically, look for a [, then repeatly take either a non-"[" and non-"]"
character or find an internal set of [...] and take everything between them; until you hit a ]. It's the later part that doesn't work as I can't seem to figure out how to get the brackets to balance properly. Is this just
something regular expression are not meant to do? Thanks for any help.

Nov 15 '05 #5
Jed,

try this:

(?:(?:^|\s*,\s* )(?<column>\[(([^,]*\[[^,]*\][^,]*)*|[^,]*)\]))

this will not work for the string you wrote, since there is a comma in this
expression: [BB[bb],CC]. is this a mistake?

if its not a mistake, get back to me and we will figure it out.

Picho

"Jed Ozone" <jed oz***@yahoo.com> wrote in message
news:OY******** ******@TK2MSFTN GP11.phx.gbl...
New to Regex and I'm having a hard time figuring this one out.

I need a regular expression what will based on balanced square brackets.
For example:
[$AA[123]], [BB[bb],CC], [a[b[c]]]

I'm trying to write a reg ex that will parse the above into 3 pieces:
1) [$AA[123]] (or $AA[123] would be fine)
2) [BB[bb],CC]
3) [a[b[c]]]

Basically, any time the square brackets balance, I want to be able to pluck out that value.

I wrote: (?<column>\[([^\[\]]*(\[.*\])*)*\])

Basically, look for a [, then repeatly take either a non-"[" and non-"]"
character or find an internal set of [...] and take everything between them; until you hit a ]. It's the later part that doesn't work as I can't seem to figure out how to get the brackets to balance properly. Is this just
something regular expression are not meant to do? Thanks for any help.

Nov 15 '05 #6
Jed,

try this:

(?:(?:^|\s*,\s* )(?<column>\[(([^,]*\[[^,]*\][^,]*)*|[^,]*)\]))

this will not work for the string you wrote, since there is a comma in this
expression: [BB[bb],CC]. is this a mistake?

if its not a mistake, get back to me and we will figure it out.

Picho

"Jed Ozone" <jed oz***@yahoo.com> wrote in message
news:OY******** ******@TK2MSFTN GP11.phx.gbl...
New to Regex and I'm having a hard time figuring this one out.

I need a regular expression what will based on balanced square brackets.
For example:
[$AA[123]], [BB[bb],CC], [a[b[c]]]

I'm trying to write a reg ex that will parse the above into 3 pieces:
1) [$AA[123]] (or $AA[123] would be fine)
2) [BB[bb],CC]
3) [a[b[c]]]

Basically, any time the square brackets balance, I want to be able to pluck out that value.

I wrote: (?<column>\[([^\[\]]*(\[.*\])*)*\])

Basically, look for a [, then repeatly take either a non-"[" and non-"]"
character or find an internal set of [...] and take everything between them; until you hit a ]. It's the later part that doesn't work as I can't seem to figure out how to get the brackets to balance properly. Is this just
something regular expression are not meant to do? Thanks for any help.

Nov 15 '05 #7
Thanks for the help Picho. The comma is not a mistake unfortunately.
Really the only rules for the syntax are the square brackets will be
balanced and there will be a comma seperating each set (outside all
brackets). Between the brackets all characters are valid along with
additional sets of square brackets. I am trying to only get the outer most
set in each case (as I showed below), as the inner brackets have a different
meaning than the outer brackets (I did not make up this syntax!).

Thanks for an additional ideas you might have.
"Picho" <pi***********@ telhai.ac.il> wrote in message
news:%2******** *******@tk2msft ngp13.phx.gbl.. .
Jed,

try this:

(?:(?:^|\s*,\s* )(?<column>\[(([^,]*\[[^,]*\][^,]*)*|[^,]*)\]))

this will not work for the string you wrote, since there is a comma in this expression: [BB[bb],CC]. is this a mistake?

if its not a mistake, get back to me and we will figure it out.

Picho

"Jed Ozone" <jed oz***@yahoo.com> wrote in message
news:OY******** ******@TK2MSFTN GP11.phx.gbl...
New to Regex and I'm having a hard time figuring this one out.

I need a regular expression what will based on balanced square brackets.
For example:
[$AA[123]], [BB[bb],CC], [a[b[c]]]

I'm trying to write a reg ex that will parse the above into 3 pieces:
1) [$AA[123]] (or $AA[123] would be fine)
2) [BB[bb],CC]
3) [a[b[c]]]

Basically, any time the square brackets balance, I want to be able to pluck
out that value.

I wrote: (?<column>\[([^\[\]]*(\[.*\])*)*\])

Basically, look for a [, then repeatly take either a non-"[" and non-"]"
character or find an internal set of [...] and take everything between

them;
until you hit a ]. It's the later part that doesn't work as I can't

seem to
figure out how to get the brackets to balance properly. Is this just
something regular expression are not meant to do? Thanks for any help.


Nov 15 '05 #8
Thanks Miha. I was hoping to break apart my whole file using regualar
expressions. I've broken up the file down to this point and was trying to
keep with regular expressions. I actually have written a procedure to do
exactly as you say below which I had planned as a temporary solution. I
actually had thought a regular expression would be able to handle something
like this fairly easily when I started, but hence, my compelete lack of
experience with them.

Now it's become more of a crusade just to see if in fact it's possible to
balanace brackets using regular expressions.

Thanks for the feedback.

"Miha Markic" <miha at rthand com> wrote in message
news:up******** ********@tk2msf tngp13.phx.gbl. ..
Hi Jed,

Why don't you create a state machine rather - iterate through string and
count [ (+1) and ] (-1) - when the count=0 and you are over a comma, you
will know that the comma is delimiter.

--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com

"Jed Ozone" <jed oz***@yahoo.com> wrote in message
news:OY******** ******@TK2MSFTN GP11.phx.gbl...
New to Regex and I'm having a hard time figuring this one out.

I need a regular expression what will based on balanced square brackets.
For example:
[$AA[123]], [BB[bb],CC], [a[b[c]]]

I'm trying to write a reg ex that will parse the above into 3 pieces:
1) [$AA[123]] (or $AA[123] would be fine)
2) [BB[bb],CC]
3) [a[b[c]]]

Basically, any time the square brackets balance, I want to be able to pluck
out that value.

I wrote: (?<column>\[([^\[\]]*(\[.*\])*)*\])

Basically, look for a [, then repeatly take either a non-"[" and non-"]"
character or find an internal set of [...] and take everything between

them;
until you hit a ]. It's the later part that doesn't work as I can't

seem to
figure out how to get the brackets to balance properly. Is this just
something regular expression are not meant to do? Thanks for any help.


Nov 15 '05 #9
Hi hemol,

Yes, I understand you. However, regular expressions are more like a pattern
matching and not parameter matching imo.
The kind of problem you are describing is just not meant to be solved with
regex IMO.
Anyway, I am curious too, if anybody comes with a solution with regex.
--
Miha Markic - DXSquad/RightHand .NET consulting & software development
miha at rthand com

Developer Express newsgroups are for peer-to-peer support.
For direct support from Developer Express, write to su*****@devexpr ess.com
Bug reports should be directed to: su*****@devexpr ess.com
Due to newsgroup guidelines, DX-Squad will not answer anonymous postings.
"hemol" <je************ *@yahoo.com> wrote in message
news:t8Hzb.2998 07$9E1.1524699@ attbi_s52...
Thanks Miha. I was hoping to break apart my whole file using regualar
expressions. I've broken up the file down to this point and was trying to
keep with regular expressions. I actually have written a procedure to do
exactly as you say below which I had planned as a temporary solution. I
actually had thought a regular expression would be able to handle something like this fairly easily when I started, but hence, my compelete lack of
experience with them.

Now it's become more of a crusade just to see if in fact it's possible to
balanace brackets using regular expressions.

Thanks for the feedback.

"Miha Markic" <miha at rthand com> wrote in message
news:up******** ********@tk2msf tngp13.phx.gbl. ..
Hi Jed,

Why don't you create a state machine rather - iterate through string and
count [ (+1) and ] (-1) - when the count=0 and you are over a comma, you
will know that the comma is delimiter.

--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com

"Jed Ozone" <jed oz***@yahoo.com> wrote in message
news:OY******** ******@TK2MSFTN GP11.phx.gbl...
New to Regex and I'm having a hard time figuring this one out.

I need a regular expression what will based on balanced square brackets. For example:
[$AA[123]], [BB[bb],CC], [a[b[c]]]

I'm trying to write a reg ex that will parse the above into 3 pieces:
1) [$AA[123]] (or $AA[123] would be fine)
2) [BB[bb],CC]
3) [a[b[c]]]

Basically, any time the square brackets balance, I want to be able to

pluck
out that value.

I wrote: (?<column>\[([^\[\]]*(\[.*\])*)*\])

Basically, look for a [, then repeatly take either a non-"[" and non-"]" character or find an internal set of [...] and take everything between

them;
until you hit a ]. It's the later part that doesn't work as I can't

seem
to
figure out how to get the brackets to balance properly. Is this just
something regular expression are not meant to do? Thanks for any help.



Nov 15 '05 #10

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

Similar topics

9
10362
by: Ron Adam | last post by:
Is it possible to match a string to regular expression pattern instead of the other way around? For example, instead of finding a match within a string, I want to find out, (pass or fail), if a string is a partial match to an re. Given an re of 'abcd and a bunch of other stuff' This is what i'm looking for:
3
2033
by: Tom | last post by:
I have struggled with the issue of whether or not to use Regular Expressions for a long time now, and after implementing many text manipulating solutions both ways, I've found that writing specialized code instead of an RE is almost always the better solution. Here is why.... RE's are complex. Sure it is one line of code, but it is on hell of a line. Some of my RE remind me of the obfuscated code contest winners, where one line of...
1
1861
by: Gareth James via .NET 247 | last post by:
I have an expression that when run uses 100% cpu for over 1minute. I can change the expression so this does not happen, but couldsome one explain why this happens so that I don't do it again expression --> Departing:</td>.*?</span>(?<departingAirportName>.*?)\( (?<airportCode>\w+)\).*?<li> (?<departingCity>+),\s*
5
2537
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, and then reconstruct another URL based on it. For example, I need to scan a web page looking for something like <a href="some_dir/list_20050815100225.csv">. I don't know in advance what the date/time in the file name will be. I need to take the...
2
7962
by: Christian Staffe | last post by:
Hi, I would like to check for a partial match between an input string and a regular expression using the Regex class in .NET. By partial match, I mean that the input string could not yet be complete but I want to know if a match is possible so far. For instance I want to design a text box to enter a date and validate the correctness of the date as the user types character. If the user enters 1953/12/23 it will match my regex of course...
0
272
by: Jed Ozone | last post by:
New to Regex and I'm having a hard time figuring this one out. I need a regular expression what will based on balanced square brackets. For example: ], ,CC], ]] I'm trying to write a reg ex that will parse the above into 3 pieces: 1) ] (or $AA would be fine) 2) ,CC] 3) ]]
4
3237
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 over each document, find out if it contains a header and/or a footer and extract only the main content part. The headers and the footers have no specific format and I have to detect and remove them using a list of strings that may appear as...
11
5399
by: Dimitris Georgakopuolos | last post by:
Hello, I have a text file that I load up to a string. The text includes certain expression like {firstName} or {userName} that I want to match and then replace with a new expression. However, I want to use the text included within the brackets to do a lookup so that I can replace the expression with the new text. For example:
14
4996
by: Andy B | last post by:
I need to create a regular expression that will match a 5 digit number, a space and then anything up to but not including the next closing html tag. Here is an example: <startTag>55555 any text</aClosingTag> I need a Regex that will get all of the text between the html tags above (the html tags are random and i do not know them before hand). The match string always starts with at least 5 digits.
0
9812
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10828
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...
1
10579
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10244
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
9364
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
7775
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
6975
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
5814
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3103
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.