Home > database >  Limit Group1 until special character
Limit Group1 until special character

Time:01-09

I have a new challenge, The emails were changed and this is not working anymore:

Regex expression: Type\sof\sContract\s:\s(.*)

Text:

Type of Contract : Permanent

And I get the match and the group1 information "Permanent"

Now the text, it comes like this:

Type of Contract : Permanent{color}{color:#000000}

And I get the match and the group1 information "Permanent{color}{color:#000000}"

How can I get again only "Permanent" In group1 ?

CodePudding user response:

You can try:

Type\sof\sContract\s:\s([^{\r\n] )

Regex demo.


This will get the string after Type\sof\sContract\s:\s until { or newline is found.

CodePudding user response:

Demo, using Perl and a capture as requested:

perl -pe 's/Type\s of\s Contract\s :\s (.*?)(?=\{).*/$1/' file

Output

Permanent

The regular expression matches as follows:

Node Explanation
Type 'Type'
\s whitespace (\n, \r, \t, \f, and " ") (1 or more times (matching the most amount possible))
of 'of'
\s whitespace (\n, \r, \t, \f, and " ") (1 or more times (matching the most amount possible))
Contract 'Contract'
\s whitespace (\n, \r, \t, \f, and " ") (1 or more times (matching the most amount possible))
: :
\s whitespace (\n, \r, \t, \f, and " ") (1 or more times (matching the most amount possible))
( group and capture to \1:
.*? any character except \n (0 or more times (matching the least amount possible))
) end of \1
(?= look ahead to see if there is:
\{ {
) end of look-ahead
.* any character except \n (0 or more times (matching the most amount possible))
  • Related