Solved

regex expression for up to but not including and split

Posted on 2010-11-16
9
426 Views
Last Modified: 2012-05-10
I have a line of text

blah blah blah;blah blah blah

I'm trying to formulate a regex expression to capture everything up to the semicolon.

On a second regex or to replace the earlier, I basically need to split up a string separated by semicolons.

Thanks in advance.
0
Comment
Question by:kblackwel
  • 6
  • 3
9 Comments
 
LVL 35

Expert Comment

by:Terry Woods
ID: 34150009
$line = preg_replace("/;.*/","",$line);
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 34150028
To split a string separated by semicolons, use:

$my_array = preg_split("/;/",$string);
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 34150035
Ah, I made an assumption you're using PHP - which language are you using?
0
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 

Author Comment

by:kblackwel
ID: 34150163
It's for a ETL, so it has to be regex
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 34150190
Ok, well the patterns I've provided are valid, but the first one requires use of a replace command rather than a capture.

To capture text before the first ; character, use pattern:

^(.*?);
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 34150218
Does the string have a fixed (or limited) number of fields? Depending on your regex tool, you might be able to use the following to split a string:

(?:([^;]*);)*

or, with a limited number of fields, something like this:

([^;]*);([^;]*);([^;]*);([^;]*);([^;]*)
0
 

Author Comment

by:kblackwel
ID: 34150535
Thank you,

For my needs, all I needed was

([^;]*)

That matched blah blah blah up to the semicolon.

The field I'm trying to parse would only have 2 semicolons in it max.

Any thoughts on matching the second blah blah blah past the first semicolon.

Again, thanks on the first solution.

Basically I'm pulling a string out of a DB table. In my etl program, I don't have access to split or anything like that. But regex is available. Trying to use that to parse an address table row that is delimited by semicolons.
0
 

Author Comment

by:kblackwel
ID: 34150542
Actually from the semicolon to end of line would be fine too.
0
 
LVL 35

Accepted Solution

by:
Terry Woods earned 125 total points
ID: 34150610
From the first semi-colon to the end of the line:

;(.*)

0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
A short article about problems I had with the new location API and permissions in Marshmallow
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question