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

Earliest instance of a character using Regex

Hi all,
More regex questions, sadly ;)

If I had a string that could contain any combination, or some/none of
[].+-*/ how could I strip off extraneous characters after any of these
characters to get just the left most sub string?
eg
Do.Bears+Defecate*Rurally would give Do
Mostly*Harmless would give Mostly
Don-t/Panic would give Don

TIA

Paul

Feb 23 '07 #1
3 1613
tr***************@btinternet.com wrote:
Hi all,
More regex questions, sadly ;)

If I had a string that could contain any combination, or some/none of
[].+-*/ how could I strip off extraneous characters after any of these
characters to get just the left most sub string?
eg
Do.Bears+Defecate*Rurally would give Do
Mostly*Harmless would give Mostly
Don-t/Panic would give Don

TIA

Paul
I believe it's easier using string methods:

s.Substring(0, s.IndexOfAny("[].+*/".ToCharArray()))

--
Göran Andersson
_____
http://www.guffa.com
Feb 23 '07 #2
On 23 Feb, 12:00, Göran Andersson <g...@guffa.comwrote:
trashman.horli...@btinternet.com wrote:
Hi all,
More regex questions, sadly ;)
If I had a string that could contain any combination, or some/none of
[].+-*/ how could I strip off extraneous characters after any of these
characters to get just the left most sub string?
eg
Do.Bears+Defecate*Rurally would give Do
Mostly*Harmless would give Mostly
Don-t/Panic would give Don
TIA
Paul

I believe it's easier using string methods:

s.Substring(0, s.IndexOfAny("[].+*/".ToCharArray()))

--
Göran Andersson
_____http://www.guffa.com- Hide quoted text -

- Show quoted text -

Thanks mate!

Feb 23 '07 #3
Hello trashman,
>If I had a string that could contain any combination, or some/none of
[].+-*/ how could I strip off extraneous characters after any of these
characters to get just the left most sub string?
eg
Do.Bears+Defecate*Rurally would give Do
Mostly*Harmless would give Mostly
Don-t/Panic would give Don
You could use this expression:

.*?[\[\]\.+\-*/]

Looks wild - you're scanning for characters that have special meaning in
regular expressions. Also, look only for the first match, as you're not
interested in the rest.

I agree with Göran's suggestion of using string functions instead - not
necessarily because it's easier, I think that's a matter of content and
personal perception, but because regular expressions always incur a
certain overhead. As a basic rule, if there's no particular reason to want
the regular expression, an alternative solution will usually be preferable
to me.
Oliver Sturm
--
http://www.sturmnet.org/blog
Feb 24 '07 #4

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

Similar topics

1
by: GlennH | last post by:
I am having trouble removing a pipe character using Regex.Replace - see the 2 NUnit tests below: The first replace works fine and the second Replace does not work. I've tried escaping the pipe...
6
by: Chris Anderson | last post by:
Anyone know of a fix (ideally) or an easy workaround to the problem of escape characters not working in regex replacement text? They just come out as literal text For example, you'd think that thi...
7
by: Chris Thunell | last post by:
I'm looking to find in a long string an instance of 4 numbers in a row, and pull out those numbers. For instance: string = "0104 PBR", i'd like to get the 0104. string="PBR XT 0105 TD", i'd like...
1
by: sonald | last post by:
Dear All, I am working on a module that validates the provided CSV data in a text format, which must be in a predefined format. We check for the : 1. Number of fields provided in the text file,...
24
by: garyusenet | last post by:
I'm working on a data file and can't find any common delimmiters in the file to indicate the end of one row of data and the start of the next. Rows are not on individual lines but run accross...
0
by: trashman.horlicks | last post by:
Hi all, More regex questions, sadly ;) If I had a string that could contain any combination, or some/none of .+-*/ how could I strip off extraneous characters after any of these characters to...
1
by: ceestand | last post by:
I'm trying to remove all whitepace in an HTML-formatted string, except within the actual content. so I want to get: <p> <b>some text</b> </p> to look like <p><b>some text</b></p> I've been...
2
by: christopher taylor | last post by:
hello python-list! the other day, i was trying to match unicode character sequences that looked like this: \\uAD0X... my issue, is that the pattern i used was returning:
5
by: shapper | last post by:
Hello, I have a text as follows: "My email is something@something.xyz and I posted this @ 2 am" I need to replace the @ by (AT) bu only the ones that are in email addresses. All other @...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
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
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,...
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
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.