regex of two patterns

Gislef

I have a very large csv list, I've already converted a list into array and managed to fix a problem I was having with UTF8:

 $lines = file(''.get_template_directory_uri() . '/lines.csv');      

        foreach ($lines as $line_num => $line)
    {
        if(mb_detect_encoding($line, 'utf-8', false)) {
            $listLines.=  $line . '<br />';     
         }
    } 

But all of the list items follow one of the two patterns below:

Fist

Adolfo (São Paulo)|Adolfo (SP)

Basically I need all content that is before |, output:

Adolfo_(São_Paulo)

second

other items in the list do not have |

Abatiá (PR)    
Abel Figueiredo (PA)
São Francisco de Assis do Piauí (PI)

I need output:

Abatiá
Abel_Figueiredo
São_Francisco_de_Assis_do_Piauí

I believe I'm going to have to use regex, but I'm a bit confused as to make the rule for both situations.

D.B.

Based on comments... how about this:

$lines = file(''.get_template_directory_uri() . '/lines.csv');      

foreach ($lines as $line_num => $line)
{
    if(mb_detect_encoding($line, 'utf-8', false)) {
        $exp = '';
        if(strpos($line, '|')!==FALSE){
            $exp = '/^(.+?)\s*\|/';
        }else{
            $exp = '/^(.+?)\s*\(/';
        }
        preg_match($exp, $line, $matches);
        if($matches){
             $line = $matches[1];
             $line = preg_replace('/\s+/', '_', $line);
             $listLines.=  $line . '<br />';
        }
    }
} 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

RegEx: Match one of two patterns

Extract two patterns at once using regex

regex with or condition to verify two different patterns?

Regex for single delimiter between two patterns

how to merge two models of regex patterns?

Regex: extract characters from two patterns

RegEx Match everything between two patterns (javascript)

Regex to find two patterns and extract values

How do I combine these two regex patterns?

Delete string between two regex patterns

Replace two patterns at the same regex in JavaScript

Regex: possibly two patterns found in one text

Regex to extract text between two character patterns

What is the different between these two RegEx patterns

How to code a Regex for a shared character between two correspondent patterns?

Python regex - Extract all the matching text between two patterns

Regex (.NET) to find a pattern between two other patterns

Extract only the portion of a string between two regex patterns

How can I force two patterns to capture to the same group in regEx

Regex findall between two optional patterns, return all if none

REGEX for matching number with two and three numbered patterns together

Use static string once in Regex while using two patterns with OR

If pattern repeats two times (nonconsecutive) match both patterns, regex

Javascript regex to match between two patterns, where first pattern is optional

Print matched regex values from two different patterns in the same line

How to Google form RegEx validation that matches with the "OR" expression in two patterns

Difference between two regex patterns to find words ending with 'ing'

Regular expression to capture n lines of text between two regex patterns

Extract lines between two patterns with awk and a variable regex

TOP Ranking

HotTag

Archive