473,396 Members | 1,927 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,396 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 1612
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: 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...
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...
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
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
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,...

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.