logo
Tags down

shadow

Regex optional capture groups in any order


By : Aidan James Connolly
Date : August 01 2020, 08:00 PM
hope this fix your issue Your text is already almost too random to make this work. Even more names and emails are very difficult to capture at times. A more advanced email pattern would only help a little.There are not only unusual email addresses there are also all sorts of wild name patterns.
What about D'arcy Bly, Markus-Anthony Reid, Lee Z, and those are probably the simplest examples.
code :
([A-Z]\w+ [A-Z]\w+)(?:\w* )*([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4})|
([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4})(?:\w* )*([A-Z]\w+ [A-Z]\w+)|
([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4})


Share : facebook icon twitter icon

Regex Optional Capture Groups


By : Mickael
Date : March 29 2020, 07:55 AM
wish help you to fix your issue Something like this: (hyundai){0,1}\s*(E&C){0,1}\s*(hillstate){0,1}\s*(\(KOR\)){0,1}\s*\W\s*(Heungkuk){0,1}(.*)

Regex with optional capture groups


By : user2345359
Date : March 29 2020, 07:55 AM
With these it helps Put the whole {...} part in an optional non-capturing group, while keeping the first group lazy:
code :
^\/(.*?)(?:{(.*)\:(.*)})?$
        ^^^             ^
^\/([^{]+)(?:{(.*)\:(.*)})?$

Parse string with Regex - optional capture groups


By : Panda Po
Date : March 29 2020, 07:55 AM
help you fix your problem You could use a pattern with an optional second part for the a abbreviation and the item code integer. The values you could capture in a capturing group giving you 2 groups and 2 optional groups.
If you want to match whitespace characters instead of a space only, you could use \s instead.
code :
\b(\d+)x? (\w+(?: \w+)*)(?: \(([^()]+)\) (\d+))?\b

Regex capture either whole line or if there is an optional character then capture also 2 capture groups


By : user3001398
Date : March 29 2020, 07:55 AM
I wish this help you I just have found a solution. With sed output configured like this "\1|\2|\3|" it will produce a delimited string and then I simply split that string by "|" character to get array, which can be compared for correct array member count and then assign them to appropriate variable for later possible processing.
code :
#!/bin/bash                                                                                                  

SUBJECT="Subject: 2019.07.19 13:30:00 some.host.com 178899 Problem: Server is unavailable"                   

OUTPUT=`echo "$SUBJECT" | sed -E 's/Subject\: ([2-9]{1}[0-9]{3}\.[0-9]{2}\.[0-9]{2}) ([0-9]{2}\:[0-9]{2}\:[0-
9]{2}) ([a-zA-Z0-9._-]*) ([0-9]*) Problem\: (.*)/\1|\2|\3|\4|\5|/'`                                          

echo "OUTPUT = $OUTPUT"                                                                                      

IFS='|'                                                                                                      
read -a RESULT <<< "$OUTPUT"                                                                                 

echo "${RESULT[0]}"                                                                                          
echo "${RESULT[1]}"                                                                                          
echo "${RESULT[2]}"                                                                                          
echo "${RESULT[3]}"                                                                                          
echo "${RESULT[4]}"  
sh-4.4$ ./test.sh                                                                                            
OUTPUT = 2019.07.19|13:30:00|some.host.com|178899|Server is unavailable|                                     
2019.07.19                                                                                                   
13:30:00                                                                                                     
some.host.com                                                                                                
178899                                                                                                       
Server is unavailable
OUTPUT="$(echo "$SUBJECT" | sed -E 's/Subject\: ([2-9]{1}[0-9]{3}\.[0-9]{2}\.[0-9]{2}) ([0-9]{2}\:[0-9]{2}\:[
0-9]{2}) ([a-zA-Z0-9._-]*) ([0-9]*) Problem\: (.*)/\1|\2|\3|\4|\5|/')"
#!/bin/bash

SUBJECT="Subject: 2019.07.19 13:30:00 some.host.com 178899 Problem: Resolved: Server Problem:is unavailable"

OUTPUT="$(echo "$SUBJECT" | sed -E 's/Subject\: ([2-9]{1}[0-9]{3}\.[0-9]{2}\.[0-9]{2}) ([0-9]{2}\:[0-9]{2}\:[
0-9]{2}) ([a-zA-Z0-9._-]*) ([0-9]*) (Problem|Resolved)\: (.*)/\1|||\2|||\3|||\4|||\5|||\6/')"

IFS='|||'

read -a RESULT <<< "$OUTPUT"

if [ "${#RESULT[*]}" -eq '16' ];
then
  EVENT_DATE="${RESULT[0]}"
  EVENT_TIME="${RESULT[3]}"
  EVENT_HOST="${RESULT[6]}"
  EVENT_ID="${RESULT[9]}"
  EVENT_TYPE="${RESULT[12]}"
  EVENT_TEXT="${RESULT[15]}"

  echo "EVENT_DATE: $EVENT_DATE"
  echo "EVENT_TIME: $EVENT_TIME"
  echo "EVENT_HOST: $EVENT_HOST"
  echo "EVENT_ID:   $EVENT_ID"
  echo "EVENT_TYPE: $EVENT_TYPE"
  echo "EVENT_TEXT: $EVENT_TEXT"
fi
EVENT_DATE: 2019.07.19
EVENT_TIME: 13:30:00
EVENT_HOST: some.host.com
EVENT_ID:   178899
EVENT_TYPE: Problem
EVENT_TEXT: Resolved: Server Problem:is unavailable

Regex to capture groups and ignore last two characters where one is optional


By : Davinder Snehi
Date : March 29 2020, 07:55 AM
I wish this helpful for you Edited based on comment to include ; and " in the comments as per the examples given:
code :
(?<key>\w+)\s*=\s*(?:[\"]?)(?<value>((")(?!;?$)|;(?!$)|[^;"])+)"?;?$
(?<key>\w+)\s*=\s*((?:")(?<valueT>((")(?!;?$)|;(?!$)|[^;"])+)";?$|(?<valueN>[^;"]+);?$)
public class NameValuePairRegex{

    public static void main( String[] args ){
        String SPACE = "\\s*";
        String EQ = "=";
        String OR = "|";

        /* The original regex tried by you (for comparison). */
        String orig = "(?<key>\\w+)\\s*=\\s*(?:[\\\"]?)(?<value>.+(?:(?=;)))";

        String key = "(?<key>\\w+)";
        String valuePatternForText = "(?:\")(?<valueT>((\")(?!;?$)|;(?!$)|[^;\"])+)\";?$";
        String valuePatternForNumbers = "(?<valueN>[^;\"]+);?$";
        String p = key + SPACE + EQ + SPACE + "(" + valuePatternForText + OR + valuePatternForNumbers + ")";

        Pattern nvp = Pattern.compile( p );
        System.out.println( nvp.pattern() );
        print( input(), nvp );
    }

    private static void print( List<String> input, Pattern ep ) {
        for( String e : input ) {
            System.out.println( e );
            Matcher m = ep.matcher( e );
            boolean found = m.find();
            if( !found ) {
                System.out.println( "\t\tNo match" );
                continue;
            }

            String valueT = m.group( "valueT" );
            String valueN = m.group( "valueN" );

            System.out.print( "\t\t" + m.group( "key" ) + " -> " + ( valueT == null ? "" : valueT ) + " " + ( valueN == null ? "" : valueN ) );
            System.out.println(  );
        }

    }

    private static List<String> input(){
        List<String> neg = new ArrayList<>();
        Collections.addAll( neg, 
                "Comment = \"This is a comment\";",
                "Comment = \"This is a comment with semicolon ;\";", 
                "Comment = \"This is a comment with semicolon ; and quote\"\";",
                "Comment = \"This is a comment\"", 
                "Comment = \"This is a \"comment\"; This is still a comment\";",
                "NumericValue = 123456;",
                "NumericValue = 123;456;",
                "NumericValue = 123\"456;",
                "NumericValue = 123456" );

        return neg;
    }

}
(?<key>\w+)\s*=\s*(?:[\"]?)(?<group>.+(?:(?=[\"]?;)))
(?<key>\w+)\s*=\s*(?:[\"]?)(?<value>[^;"]+)
shadow
Privacy Policy - Terms - Contact Us © voile276.org