Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression: Match everything after a particular word

Tags:

python

regex

I am using Python and would like to match all the words after test till a period (full-stop) or space is encountered.

text = "test : match this."

At the moment, I am using :

import re
re.match('(?<=test :).*',text)

The above code doesn't match anything. I need match this as my output.

like image 645
Amith Avatar asked May 19 '15 13:05

Amith


People also ask

How do you match everything after a word in regex?

Method 1: Match everything after first occurence Whitespace characters include spaces, tabs, linebreaks, etc. while non-whitespace characters include all letters, numbers, and punctuation. So essentially, the \s\S combination matches everything.

How do you match a word in regex?

To run a “whole words only” search using a regular expression, simply place the word between two word boundaries, as we did with ‹ \bcat\b ›. The first ‹ \b › requires the ‹ c › to occur at the very start of the string, or after a nonword character.

How do you match everything including newline regex?

The dot matches all except newlines (\r\n). So use \s\S, which will match ALL characters.

What is ?= * In regular expression?

. Your regex starts with (?= (ensure that you can see, but don't consume) followed by . * (zero or more of any character).


1 Answers

Everything after test, including test

test.*

Everything after test, without test

(?<=test).*

Example here on regexr.com

like image 176
Punnerud Avatar answered Oct 20 '22 20:10

Punnerud