473,385 Members | 1,492 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,385 software developers and data experts.

RegEx search for a substring within a substring

How can I use regular expressions to search for a substring within
another substring? For example, if I have the string:

"This is * some text <button id="123">Some * text</button>"

and I only wanted to find the * character(s) that appear within the
<button> tags, how could I do that using reg ex?

Thanks in advance for the help.

Nov 17 '05 #1
3 2292
co*********@gmail.com wrote:
How can I use regular expressions to search for a substring within
another substring? For example, if I have the string:

"This is * some text <button id="123">Some * text</button>"

and I only wanted to find the * character(s) that appear within the
<button> tags, how could I do that using reg ex?


You'll need to write a regular expression that uses balanced matching,
to make sure that any given * is actually within the scope defined by
the button tag. Have a look at this blog article for information on
balanced matching:

http://blogs.msdn.com/bclteam/archiv...15/396452.aspx
Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
Nov 17 '05 #2
Oliver Sturm wrote:
You'll need to write a regular expression that uses balanced matching,
to make sure that any given * is actually within the scope defined by
the button tag.


Or, thinking about it, you don't want to do that at all, because the
button tag can't be nested at all. In that case, try something like this:

<button[^>]*>(.*?(?<asterisk>\*).*?)*</button>

Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
Nov 17 '05 #3
co*********@gmail.com writes:
How can I use regular expressions to search for a substring within
another substring? For example, if I have the string:

"This is * some text <button id="123">Some * text</button>"

and I only wanted to find the * character(s) that appear within the
<button> tags, how could I do that using reg ex?

Thanks in advance for the help.


Are you trying to parse HTML, SGML, XML, or some other markup language
using regular expressions? If so, the Perl FAQ will tell you why that's
a bad idea. Use a parser for parsing.

Also - why the cross-post? Regular expressions are not generic - so an
answer you get from the Perl group won't necessarily apply to a regex
engine that's written in C#, and vice versa. If you have a question about
C# regexes, ask it in the C# group.

sherm--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
Nov 17 '05 #4

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

Similar topics

3
by: Day Of The Eagle | last post by:
Jeff_Relf wrote: > ...yet you don't even know what RegEx is. > I'm looking at the source code for mono's Regex implementation right now. You can download that source here ( use the class...
3
by: Timmy | last post by:
I'm working on a simple click-through image gallery and I have images with captions in two arrays like this: var current_value="0"; var images = new Array ("photo01.jpg", "photo02.jpg",...
7
by: alphatan | last post by:
Is there relative source or document for this purpose? I've searched the index of "Mastering Regular Expression", but cannot get the useful information for C. Thanks in advanced. -- Learning...
7
by: melanieab | last post by:
Hi, I'm trying to use DataView to find the row number in the datatable that contains "Rich" in it so that I can highlight it. It works fine when I enter the entire string (i.e. Richard), but I...
8
by: bonit99 | last post by:
hello! I have a question on how I can replace all occurences of one special character within a matching string? for example string: key = 'som#ething#something' OR anotherkey <= 'anot#her'...
16
by: Mark Chambers | last post by:
Hi there, I'm seeking opinions on the use of regular expression searching. Is there general consensus on whether it's now a best practice to rely on this rather than rolling your own (string)...
4
by: Flomo Togba Kwele | last post by:
I am having difficulty writing a Regex constructor. A line has a quote(") at its beginning and its end. I need to strip both characters off. If the line looks like "1", I need the result to be 1....
1
by: jonnyboy6969 | last post by:
Hi All Really hoping someone can help me out here with my deficient regex skills :) I have a function which takes a string of HTML and replaces a term (word or phrase) with a link. The pupose...
3
by: =?Utf-8?B?anAybXNmdA==?= | last post by:
Two part question: 1. Is Regex more efficient than manually comparing values using Substring? 2. I've never created a Regex expression. How would I use regex to do the equivalent of what I...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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...

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.