Regex - Get all characters after each instance of specific character

I am very new to regex and just cannot figure out how to write a pattern to match what I need. Any help would be awesome!

I want to use PHP & regex to capture each set of characters in a string that follow a specific unique character (delimiter), plus any set of characters that precedes the first instance of that delimiter. I then want to "match" the desired output into a PHP array.

  • Example delimiter: >
  • Example string:

    $str = 'word1 > word-2 > word.3 > word*4';

  • My desired match:

    array([0] => 'word1', [1] => 'word-2', [2] => 'word.3', [3] => 'word*4',);

I've looked through the following responses, and while they are close, they don't quite help me achieve what I need:

  • get all characters after "," character
  • Regex to get the words after matching string
  • Regex to get everything after the first space

This is the PHP function I'm currently working with, but it currently only finds the characters between the delimiter:

function parse_conditions($str, $delimiter='>') { if (preg_match_all('/' . $delimiter . '(.*?)' . $delimiter . '/s', $str, $matches)) { return $matches[1]; }

NOTE: the number of items in a given string may vary, so I can't use a pattern that expects a specific number of delimiters (ex. /^(.*?)>(.*?)>(.*?)>$/)


I would use preg_split,, for this.

$matches = preg_split('~\s*>\s*~', 'word1 > word-2 > word.3 > word*4');


[0] => word1
[1] => word-2
[2] => word.3
[3] => word*4

The \s* means any number of whitespace characters.

This is similar to using an explode.

$matches = explode('>', 'word1 > word-2 > word.3 > word*4');

but as you see with the explode you have the whitespaces:

[0] => word1
[1] => word-2
[2] => word.3
[3] => word*4

You can simply use array_map along with explode as

$str = 'word1 > word-2 > word.3 > word*4';
$result = array_map('trim', explode('>', $str));


[0] => word1
[1] => word-2
[2] => word.3
[3] => word*4

You can Check it here

As mentioned, you could just use explode for this:

$str = 'word1 > word-2 > word.3 > word*4';
print_r(explode(" > " , $str));

However for completeness sake, let's also use RegEx.

In this case, we can tell the Regular Expression to group all characters together that aren't whitespace and aren't the delimiter >:

preg_match_all('/([^>\s]+)/', $str, $matches);
echo print_r($matches[0]);

# [0] => Array
# (
# [0] => word1
# [1] => word-2
# [2] => word.3
# [3] => word*4
# )

Category:php Time:2018-08-10 Views:0
Tags: php regex split

Related post

  • get all characters after "," character 2010-10-30

    Need a Regex to get all characters after , (not including it) from a variable. This variable can contain for example 'SELECT___100E___7',24 'SELECT___100E___7',1 'SELECT___100E___7',286 'SELECT___100E___7',5147 Note: There can be any length of charac

  • get all text after single instance of delimiter 2011-08-12

    I'm trying to work out a graceful way of solving this problem. I have a long string in a variable with \brd separating some values (from another program) I need to get all text after the first instance of this delimiter. ie $foo = 'alpha \brd bravo \

  • RegEx get all characters before 2010-07-15

    I have this regular expression to get urls: (((ht|f)tp(s?))\://)?(www.|[a-zA-Z].)[a-zA-Z0-9-.]+.(com|edu|gov|mil|net|org|biz|info|name|museum|us|ca|uk)(\:[0-9]+)*(/($|[a-zA-Z0-9.\,\;\?\'\+&%\$#\=~_-]+))* And I want to modify it so that when I cal

  • regex match all characters after open bracket [ 2011-09-02

    I am using a regex /(?<=\[)(.*)/i to match every character after a [. A user will type something like: hello this is [what i want I want it it match what i want. It seems to work on a regex tester: When I use it in my c

  • Regex: replace all characters after 15th with '-' 2008-12-09

    I am trying to do some simple formatting stuff with 'sed' in linux, and i need to use a regex to trim a string after the 15th character, and append a '...' to the end. Something like this: before: this is a long string that needs to be shortened afte

  • Powershell/Regex: Get 8 Characters after given String 2011-03-22

    I have a Textfile containig Random Text Test: abcd1234(/cxcd) How do i extract the Random value after Test: with Powershell using Regex I always need the 8 chars after "Test: " --------------Solutions------------- $text -match "Test: (.{8})" Your mat

  • Not getting all characters after reading from file 2011-10-14

    #include<stdio.h> #include<stdlib.h> int main() { int count=0; char c=0; printf("Reading this Source file\nFile name is: %s\n",__FILE__); FILE *myFile=fopen(__FILE__,"r"); if(myFile) printf("File Successfully Opened\n"); do{ c=fgetc(myFil

  • Substring formatting to get all characters after the first underscore but before the 2nd underscore? 2011-12-28

    for example we have this string: hello_my name_is_bob and want to get only the "my Name" portion of the string, how could I get this simply with substring? Also, the format in the example will always be the same so I just need to retrieve what is aft

  • Replace all characters after the 10th inside specific tag 2012-01-11

    I think I have the basics of this script but cant nail down why it isnt working. I have an anchor which is dynamic. Due to my design parameters if it is over 10 characters it is cut off, so I want to truncate it with a hellip after the 10th character

  • Choose all characters to the right of specific character 2014-07-24

    I have a field in a table that I need to split in a query. I need to parse all of the characters to the right of a period. For example the number 10540.214 needs to be separated into 10540 and 214 while the number 10202.3012 needs to be separated int

  • Remove all characters after last - but only if after - has four characters 2010-11-17

    I have a string which could contain several different values, among them are. EDITED for clarity: var test could equal FW21002-185 or FW21002-181-0001 or abcdefg or 245-453-654 or FW21002-181-00012 I would like to remove all characters after and incl

  • How to remove all characters after a specific character in python? 2009-05-24

    I have a string. How do I remove all text after a certain character? (In this case ...) The text after will ... change so I that's why I want to remove all characters after a certain one. --------------Solutions------------- Split on your separator a

  • get all characters to right of last dash 2011-03-16

    I have the following: string test = "9586-202-10072" How would I get all characters to the right of the final - so 10072. The number of characters is always different to the right of the last dash. How can this be done? --------------Solutions-------

  • How to get all textNodes in HTML document from specific Tags using javascript? 2010-12-06

    How to get all textNodes from a group of specific tags say "blockquote,em,h4,h6,p" in a single array without using xpath or treewalker (IE doesn't allow you to use xpath & treewalker)..please help me...its ok if the script is IE only. -----------

  • How to get all combinations of a string with fixed character position? 2011-09-07

    I need to find a way to produce all combinations of a string with a particular character to always display in PHP. For example, given a string 'ABCD', and I want to get all combinations of the string with the character 'B' present, I want to get: arr

  • how to get all the page links within a specific site? 2011-12-12

    I want to program in php to get all the page links within my site as i want to check the pagerank for each page of my site, is there a tool or a library or an implemented algorithm in php to get all the page links of a specific site? --------------So

  • Get all mails in outlook from a specific folder 2011-04-14

    I would like to get all mails in outlook from a specific folder: My folder structure: I find this Code to get all mails in outlook inbox folder (not the inbox in picture!): Outlook.Application oApp; Outlook._NameSpace oNS; Outlook.MAPIFolder oFolder;

  • Get all Facebook friends that like a specific page 2014-09-11

    is there an easy way to get all Facebook friends that like a specific Facebook page? I know that there is this site here Where you can type in the page_id and get all friends t

  • use regex or php function to get all characters before ":" (colon) 2011-08-02

    I have many fields that are like this pattern: re2man: (johnny bravo) re1man: (john smith)...... user: (firstname lastname).. I wanted to use regex, or another php function to get only characters before the colon (":") --------------Solutions--------

Copyright (C), All Rights Reserved.

processed in 1.322 (s). 13 q(s)