Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 452
  • Last Modified:

regex expression for up to but not including and split

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
kblackwel
Asked:
kblackwel
  • 6
  • 3
1 Solution
 
Terry WoodsIT GuruCommented:
$line = preg_replace("/;.*/","",$line);
0
 
Terry WoodsIT GuruCommented:
To split a string separated by semicolons, use:

$my_array = preg_split("/;/",$string);
0
 
Terry WoodsIT GuruCommented:
Ah, I made an assumption you're using PHP - which language are you using?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
kblackwelAuthor Commented:
It's for a ETL, so it has to be regex
0
 
Terry WoodsIT GuruCommented:
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
 
Terry WoodsIT GuruCommented:
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
 
kblackwelAuthor Commented:
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
 
kblackwelAuthor Commented:
Actually from the semicolon to end of line would be fine too.
0
 
Terry WoodsIT GuruCommented:
From the first semi-colon to the end of the line:

;(.*)

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now