Getting the Text That Follows After the Regex Match

Getting the text that follows after the regex match

You can do this with "just the regular expression" as you asked for in a comment:

(?<=sentence).*

(?<=sentence) is a positive lookbehind assertion. This matches at a certain position in the string, namely at a position right after the text sentence without making that text itself part of the match. Consequently, (?<=sentence).* will match any text after sentence.

This is quite a nice feature of regex. However, in Java this will only work for finite-length subexpressions, i. e. (?<=sentence|word|(foo){1,4}) is legal, but (?<=sentence\s*) isn't.

Regex to get the words after matching string

The following should work for you:

[\n\r].*Object Name:\s*([^\n\r]*)

Working example

Your desired match will be in capture group 1.


[\n\r][ \t]*Object Name:[ \t]*([^\n\r]*)

Would be similar but not allow for things such as " blah Object Name: blah" and also make sure that not to capture the next line if there is no actual content after "Object Name:"

Regex to extract text after a pattern

You are including the : and *** that's in the group so they will come in the output.
This should work:

.+\:\s\W+(.+)$

Check its demo here.

Regular expression: Match everything after a particular word

I don't see why you want to use regex if you're just getting a subset from a string.

This works the same way:

if line.startswith('test:'):
print(line[5:line.find('.')])

example:

>>> line = "test: match this."
>>> print(line[5:line.find('.')])
match this

Regex is slow, it is awkward to design, and difficult to debug. There are definitely occassions to use it, but if you just want to extract the text between test: and ., then I don't think is one of those occasions.

See: https://softwareengineering.stackexchange.com/questions/113237/when-you-should-not-use-regular-expressions

For more flexibility (for example if you are looping through a list of strings you want to find at the beginning of a string and then index out) replace 5 (the length of 'test:') in the index with len(str_you_looked_for).

Regex to extract only text after string and before space

you can have positive lookbehind in your pattern.

 (?<=BookTitle:).*?(?=\s)

For more info: Lookahead and Lookbehind Zero-Width Assertions

Using Regex selecting text match everything after a word or patterns (similar topic but text is not fix patterns except 1 character)

  • Ctrl+H
  • Find what: v\d[0-9a-z]*\K.*$
  • Replace with: LEAVE EMPTY
  • UNCHECK Match case
  • CHECK Wrap around
  • CHECK Regular expression
  • UNCHECK . matches newline
  • Replace all

Explanation:

v               # a "v"
\d # a digit
[0-9a-z]* # 0 or more alphanum
\K # forget all we have seen until this position
.* # 0 or more any character but newline
$ # end of line

Screenshot (before):

Sample Image

Screenshot (after):

Sample Image

regex match after word

You can use lookbehind. Then, the text in the lookbehind group isn't part of the whole match. You can see it as an anchor like \b, ^, etc.

You then get:

(?<=^BEGIN_TAG:\W)(\w.*)$

Explained:

(?<=             # Positive lookbehind group
^ # Start of line / string
BEGIN_TAG: # Literal
\W # A non-word character ([^a-zA-Z_])
)
( # First and only matching group (probably not needed)
\w # A word character ([a-zA-Z_])
.* # Any character, any number of times
)
$ # End of line / string

Getting the text till a particular character after the regex match in java

Use a lookbehind,

(?<=\\?ref_=xoxoxo\"><img src=\"\\*)[^\"]*

Code:

String s = "*?ref_=xoxoxo\"><img src=\"*http://www.wow-how.com/yaba-daba-doo.jpg\"";
Pattern regex = Pattern.compile("(?<=\\?ref_=xoxoxo\"><img src=\"\\*)[^\"]*");
Matcher matcher = regex.matcher(s);
while(matcher.find()){
System.out.println(matcher.group(0));
}

Output:

http://www.wow-how.com/yaba-daba-doo.jpg

Javascript Regexp - Match Characters after a certain phrase

You use capture groups (denoted by parenthesis).

When you execute the regex via match or exec function, the return an array consisting of the substrings captured by capture groups. You can then access what got captured via that array. E.g.:

var phrase = "yesthisismyphrase=thisiswhatIwantmatched"; 
var myRegexp = /phrase=(.*)/;
var match = myRegexp.exec(phrase);
alert(match[1]);

or

var arr = phrase.match(/phrase=(.*)/);
if (arr != null) { // Did it match?
alert(arr[1]);
}


Related Topics



Leave a reply



Submit