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

Yet another Regexp question

I'm trying to get the regular expression which matches text between two
underscores:
_MATCHTHIS_

The problem is, I don't want to match it if the only thing in between
the underscores are spaces or other underscores:
_ _ _
(this should get no match)

_hello world_
(this should match "hello world" even though there's a space because
there's also things besides a space and underscore)

Right now, I've got this:
/_([^_]+?)_/ which does well at excluding _

I've also tried this:
/_([^_\s]+?)_/ which does well if there are NO spaces between the _'s.
ie:
_hello_
(correctly matches "hello")
_hello world_
(does not match "hello world" as I would like)
_ _ _
(correctly does not match this)

I'm stumped. Any help?

Aug 23 '06 #1
5 1214
Rik
HaggMan wrote:
I'm trying to get the regular expression which matches text between
two underscores:
_MATCHTHIS_

The problem is, I don't want to match it if the only thing in between
the underscores are spaces or other underscores:
_ _ _
(this should get no match)

_hello world_
(this should match "hello world" even though there's a space because
there's also things besides a space and underscore)

Right now, I've got this:
/_([^_]+?)_/ which does well at excluding _

I've also tried this:
/_([^_\s]+?)_/ which does well if there are NO spaces between the _'s.
ie:
_hello_
(correctly matches "hello")
_hello world_
(does not match "hello world" as I would like)
_ _ _
(correctly does not match this)
/_(\s*(:?[^_\s]+\s*)+)_/

Grtz,
--
Rik Wasmus
Aug 23 '06 #2
HaggMan wrote:
I'm trying to get the regular expression which matches text between two
underscores:
_MATCHTHIS_

The problem is, I don't want to match it if the only thing in between
the underscores are spaces or other underscores:
_ _ _
(this should get no match)

_hello world_
(this should match "hello world" even though there's a space because
there's also things besides a space and underscore)

Right now, I've got this:
/_([^_]+?)_/ which does well at excluding _

I've also tried this:
/_([^_\s]+?)_/ which does well if there are NO spaces between the _'s.
ie:
_hello_
(correctly matches "hello")
_hello world_
(does not match "hello world" as I would like)
_ _ _
(correctly does not match this)

I'm stumped. Any help?
Is _hello_world_ a match or not? The following pattern might be what
you need:

/\b_\B(.+?)\B_\b/

Boundary conditions are used here instead of conditions on what's
inside.

Aug 23 '06 #3
Thank you to both of you! I'll try them out tonight.

_hello_world_ should match "hello" but not "world_" though it should
technically *never be used that way in what I'm doing.

Chung Leong wrote:
HaggMan wrote:
I'm trying to get the regular expression which matches text between two
underscores:
_MATCHTHIS_

The problem is, I don't want to match it if the only thing in between
the underscores are spaces or other underscores:
_ _ _
(this should get no match)

_hello world_
(this should match "hello world" even though there's a space because
there's also things besides a space and underscore)

Right now, I've got this:
/_([^_]+?)_/ which does well at excluding _

I've also tried this:
/_([^_\s]+?)_/ which does well if there are NO spaces between the _'s.
ie:
_hello_
(correctly matches "hello")
_hello world_
(does not match "hello world" as I would like)
_ _ _
(correctly does not match this)

I'm stumped. Any help?

Is _hello_world_ a match or not? The following pattern might be what
you need:

/\b_\B(.+?)\B_\b/

Boundary conditions are used here instead of conditions on what's
inside.
Aug 23 '06 #4
HaggMan wrote:
Thank you to both of you! I'll try them out tonight.

_hello_world_ should match "hello" but not "world_" though it should
technically *never be used that way in what I'm doing.
Then my pattern won't work.

Aug 23 '06 #5
Well, Rik's worked. Thank you both!
Chung Leong wrote:
HaggMan wrote:
Thank you to both of you! I'll try them out tonight.

_hello_world_ should match "hello" but not "world_" though it should
technically *never be used that way in what I'm doing.

Then my pattern won't work.
Aug 24 '06 #6

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

Similar topics

1
by: python_charmer2000 | last post by:
I want to match several regexps against a large body of text. What I have so far is similar to this: re1 = <some regexp> re2 = <some regexp> re3 = <some regexp> big_re = re.compile(re1 +...
19
by: Magnus Lie Hetland | last post by:
I'm working on a project (Atox) where I need to match quite a few regular expressions (several hundred) in reasonably large text files. I've found that this can easily get rather slow. (There are...
5
by: Lukas Holcik | last post by:
Hi everyone! How can I simply search text for regexps (lets say <a href="(.*?)">(.*?)</a>) and save all URLs(1) and link contents(2) in a dictionary { name : URL}? In a single pass if it could....
4
by: Jon Maz | last post by:
Hi All, I want to strip the accents off characters in a string so that, for example, the (Spanish) word "práctico" comes out as "practico" - but ignoring case, so that "PRÁCTICO" comes out as...
3
by: Sped Erstad | last post by:
There must be a simple regexp reason for this little question but it's driving me nuts. Below is a simple regexp to determine if a string contains only numbers. I'm running these two strings...
2
by: Bill McCormick | last post by:
Hello, I'm new to VB.NET but have used regexp in Perl and VI. I'd like to read a regular expression from a file and apply it to a string read from another file. The regexp is simple word...
26
by: Matt Kruse | last post by:
Are there any current browsers that have Javascript support, but not RegExp support? For example, cell phone browsers, blackberrys, or other "minimal" browsers? I know that someone using Netscape...
7
by: Csaba Gabor | last post by:
I need to come up with a function function regExpPos (text, re, parenNum) { ... } that will return the position within text of RegExp.$parenNum if there is a match, and -1 otherwise. For...
11
by: HopfZ | last post by:
I coudn't understand some behavior of RegExp.test function. Example html code: ---------------- <html><head></head><body><script type="text/javascript"> var r = /^https?:\/\//g;...
8
by: Darryl Kerkeslager | last post by:
Currently I am using the RegExp object to parse a large dataset in an Access table - but this table was exported from SQL Server, and the very correct question was asked - why not just do it in SQL...
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: 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
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
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
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...

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.