473,376 Members | 985 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,376 software developers and data experts.

regular expression to match substring xxx and not substring yyy

Hi,

Any idea about how to write a regular expression that matches a
substring xxx as long as the string does NOT contain substring yyy?

Thanks.

Kong

Dec 8 '05 #1
6 9048
<li****@email.com> wrote:
Any idea about how to write a regular expression that matches a
substring xxx as long as the string does NOT contain substring yyy?


My first thoughts would be:

1) Do you definitely need to use regular expressions? Are you matching
actual *patterns*, or just straight substrings?

2) If you *do* definitely need regular expressions, do you definitely
need to use a single one rather than checking it in two passes?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 8 '05 #2
li****@email.com wrote:
Hi,

Any idea about how to write a regular expression that matches a
substring xxx as long as the string does NOT contain substring yyy?

Thanks.

Kong


What do you want, and what do you not want to match? As Jon said, if
you need regular expressions, we can help you, but if you just need to
match a particlar string of letters, there are more readable ways to do it.
Dec 9 '05 #3

Jon,

1. I do need to match pattern (and I just used substring as a simple
example).
2. I am hoping to have a one pass (due to limitations of an existing
software).

The idea situation is to match pattern (i.e., match pattern1 but not
match pattern2) in one regular expression. The matched values of these
two patterns do not overlap, if this makes life easier.

I have struggled with lookahead, lookbehind, greedy/non-greedy, etc. in
RE without any luck.

Any pointers would be appreciated.

Thanks.

Kong

Jon wrote:
<li****@email.com> wrote:
Any idea about how to write a regular expression that matches a
substring xxx as long as the string does NOT contain substring yyy?


My first thoughts would be:

1) Do you definitely need to use regular expressions? Are you matching
actual *patterns*, or just straight substrings?

2) If you *do* definitely need regular expressions, do you definitely
need to use a single one rather than checking it in two passes?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Dec 9 '05 #4
<li****@email.com> wrote:
1. I do need to match pattern (and I just used substring as a simple
example).
2. I am hoping to have a one pass (due to limitations of an existing
software).

The idea situation is to match pattern (i.e., match pattern1 but not
match pattern2) in one regular expression. The matched values of these
two patterns do not overlap, if this makes life easier.

I have struggled with lookahead, lookbehind, greedy/non-greedy, etc. in
RE without any luck.

Any pointers would be appreciated.


Hmm... I'll have a little look when I get some time. I'm afraid I'm not
an expert on these matters...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 9 '05 #5
On 8 Dec 2005 14:21:10 -0800, li****@email.com wrote:
Hi,

Any idea about how to write a regular expression that matches a
substring xxx as long as the string does NOT contain substring yyy?

Thanks.

Kong


You posted elsewhere in this thread that xxx and yyy doesn't overlap.
That simplifies the problem. By using negative assertions it is
possible with the following expression:

(?<! .*yyy.*) #Don't match if yyy exists before xxx
xxx
(?! .*yyy.*) #Don't match if yyy exists after xxx

Note: RegexOptions.IgnorePatternWhitespace is used for readability

--
Marcus Andrén
Dec 9 '05 #6
It seems to work. Thanks!

Kong

Marcus Andrén wrote:
On 8 Dec 2005 14:21:10 -0800, li****@email.com wrote:
Hi,

Any idea about how to write a regular expression that matches a
substring xxx as long as the string does NOT contain substring yyy?

Thanks.

Kong


You posted elsewhere in this thread that xxx and yyy doesn't overlap.
That simplifies the problem. By using negative assertions it is
possible with the following expression:

(?<! .*yyy.*) #Don't match if yyy exists before xxx
xxx
(?! .*yyy.*) #Don't match if yyy exists after xxx

Note: RegexOptions.IgnorePatternWhitespace is used for readability

--
Marcus Andrén


Dec 9 '05 #7

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...
9
by: Holden Caulfield | last post by:
Hello! Does anyone know a good archive for all different types of Regular expressions? I am 90% done a website and the last 10% is putting validation on over 100 fields. They range from...
3
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...
2
by: Jonas | last post by:
I got a string from which I want to extract some info. The string has a format like this "$MyINFO $ALL %s %s$ $%s$%s$%s$|" ie "$MyINFO $ALL smurf hmm$ $LAN(T3)$yes@mail.no$85899345920$|" for doing...
10
by: Lee Kuhn | last post by:
I am trying the create a regular expression that will essentially match characters in the middle of a fixed-length string. The string may be any characters, but will always be the same length. In...
8
by: Frank | last post by:
Hi, reg expressions are very handy but it's not my speciality. Can someone help me constructing one for the next problem? PxxyyPxxyyPxxyyy Thats the string I have to split. The Pxx is always...
1
by: ENIZIN | last post by:
Hello, I'm having a bit of trouble creating my regular expression and need a guru's help! Here's what I have...I have a sequence of characters that need to be validated against the database....
7
by: Kevin CH | last post by:
Hi, I'm currently running into a confusion on regex and hopefully you guys can clear it up for me. Suppose I have a regular expression (0|(1(01*0)*1))* and two test strings: 110_1011101_ and...
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...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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.