473,750 Members | 2,680 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[Regular Expression] match a word with interpunctuatio n

teo
[Regular Expression] match word with interpunctuatio n

Hallo,
I need to build a regular expression
able to match a specified word,
preceded and followed by few chars (mainly interpunctuatio n)
Below the code.

------------------------

Explanation:

let's assume the word is
baby
these are the 27 cases I need to match:

baby baby is simply standing alone (obviously)

and also these cases:

baby.
baby,
baby;
baby:
baby!
baby?
baby baby with a following blank space
baby-
baby"
baby'
baby\
baby/
baby)

and also these cases:
..Baby
,baby
;baby
:baby
!baby
?baby
baby baby with a preceding blank space
-baby
"baby
'baby
\baby
/baby
(baby

and also
the resulting regexp
must exclude the following cases:
babylon baby followed by any char
lonbaby baby preceded by any char
baby678 baby followed by any number
678baby baby precede by any number

-------------------

This is the code:
'I have to count all the the occurences in a long text.
'I tried this code, but it didn't work:

Dim myCounter As Integer = 0

Dim myMatch As Match = Nothing

Dim myLongText As String = "This morning ...blah...blah. ... goodnight to
everyone"

Dim myPattern As String = "[.;:/,-"!\?'( ]" & "baby" & "[.;:/,-"!\?') ]"


myMatch = Regex.Match(myL ongText, myPattern, RegexOptions.Mu ltiline Or
RegexOptions.Ig noreCase)
Do While myMatch.Success
myCounter = myCounter + 1
myMatch = myMatch.NextMat ch
Loop

Debug.Write (myCounter.tost ring)

Jun 26 '06 #1
2 1931
On Tue, 27 Jun 2006 01:58:22 +0200, teo <te*@inwind.i t> wrote:

This is the code:
'I have to count all the the occurences in a long text.
'I tried this code, but it didn't work:

Dim myCounter As Integer = 0

Dim myMatch As Match = Nothing

Dim myLongText As String = "This morning ...blah...blah. ... goodnight to
everyone"

Dim myPattern As String = "[.;:/,-"!\?'( ]" & "baby" & "[.;:/,-"!\?') ]"


Does it specificaly have to be that punctuation or can it be any
non-alphanumeric character?

If so, some thing like:
"\b" & word & "\b" should work.

See "regular expressions, atomic zero-width assertions" in the help.
Jun 27 '06 #2
teo
>>
Dim myPattern As String = "[.;:/,-"!\?'( ]" & "baby" & "[.;:/,-"!\?') ]"


Does it specificaly have to be that punctuation or can it be any
non-alphanumeric character?

If so, some thing like:
"\b" & word & "\b" should work.


I specificaly have to match that punctuaction.
It seems I have solved it
with this regexp:

[^\w] [\\\,;:!&?("'./- ] ? baby [\\\,;:!&?)"'/.- ] ? [^\w]

----------------------------

I have another question:

I want to extract a given sequence,

the sequence occurs many times,

but I'm satisfied just
when I've found the very first one,

so to save computational time,
I'd like to tell to the RegExp to "stop and exit immediately" the search,
and give me the sequence,
I repeat in order to save time.

Is there any command to append to the RegExp?
Jun 27 '06 #3

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

Similar topics

1
4179
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 regular expressions easier to create and use (and in my experience as a regular expression user, it makes them MUCH easier to create and use.) I'm still working on formal documentation, and in any case, such documentation isn't necessarily the...
4
1607
by: peterbe | last post by:
I want to match a word against a string such that 'peter' is found in "peter bengtsson" or " hey peter," or but in "thepeter bengtsson" or "hey peterbe," because the word has to stand on its own. The following code works for a single word: def createStandaloneWordRegex(word): """ return a regular expression that can find 'peter' only if it's written alone (next to space, start of string, end of string, comma, etc) but
3
2145
by: Ryan Taylor | last post by:
Hello. I am trying to create a regular expression that will let me know if a string has the following criteria. Order does not matter in the string, but when building a regular expression it does, right? I am brand new to regular expressions and having a little (read: a lot of) difficulty in making a regular expression that makes sure the string meets the following rules. The string contains at least one character from two of the...
12
286
by: Kevin | last post by:
Hi, Is anyone in this group good at regular expression? I have a seemingly simple problem, but cannot figure it out myself. I appreciate any help! What I want to check is "dog" appear exactly twice in the searchString. If I enter "^(.*dog.*){2}$", the expression matches even when "dog" appear 3 times. I want to check if "dog" appear exactly 2 times. Regex regEx = new Regex("???");
3
1219
by: Craig | last post by:
Hi I'm having some troubles getting my regex to work. I have a string as follows The "quick and brown" fox "jumped over the" lazy dog. The output should be as follows: The "quick and brown" fox "jumped and over and the" lazy dog. So the expression needs to insert 'and' between the groups of words enclosed in double quotes, ignoring the insert if the 'and' word exists bewteen the
3
2565
by: Zach | last post by:
Hello, Please forgive if this is not the most appropriate newsgroup for this question. Unfortunately I didn't find a newsgroup specific to regular expressions. I have the following regular expression. ^(.+?) uses (?!a spoon)\.$
25
5165
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 (CONDUCTION DEFECT) 37.33/2 HEART (CONDUCTION DEFECT) WITH CATHETER 37.34/2 " the expression is "HEART (CONDUCTION DEFECT)". How do I gain access to the expression (not the matches) at runtime? Thanks, Mike
0
6189
by: altavim | last post by:
Usually when you make regular expression to extract text you are starting from simple expression. When you got to know target text, you are extending your expression. Subsequently very hard to ready long set of special symbols and impossible to improve such expression. We have to create ’smart’ regular expression. Instead of write one line expression we prepare multi line text from which we shall generate our long expression. Here is a simple...
1
1591
by: AAaron123 | last post by:
I found this on the Internet and tried a few of them and they worked in VS2008. Actually it was in a different form but I converted to make a smaller file. The data is the same as the original. I'm confused about how regular expressions work in different systems. I suspect that each system may have some things that do not work in other systems. So my question is: Do the things in the table below work on VS2008? And what is Posix and...
0
9001
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
8838
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
9583
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
9396
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
9256
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
8263
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...
0
6081
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
4716
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...
0
4888
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.