Expression After Last Specific Character

Regular Expression for getting everything after last slash

No, an ^ inside [] means negation.

[/] stands for 'any character in set [/]'.

[^/] stands for 'any character not in set [/]'.

Regular Expression to collect everything after the last /

This matches at least one of (anything not a slash) followed by end of the string:

[^/]+$



Notes:

  • No parens because it doesn't need any groups - result goes into group 0 (the match itself).
  • Uses + (instead of *) so that if the last character is a slash it fails to match (rather than matching empty string).



But, most likely a faster and simpler solution is to use your language's built-in string list processing functionality - i.e. ListLast( Text , '/' ) or equivalent function.

For PHP, the closest function is strrchr which works like this:

strrchr( Text , '/' )

This includes the slash in the results - as per Teddy's comment below, you can remove the slash with substr:

substr( strrchr( Text, '/' ), 1 );

Extract all text after last occurrence of a special character

We can match one or more characters that are not a | ([^|]+) from the start (^) of the string followed by | in str_remove to remove that substring

library(stringr)
str_remove(str1, "^[^|]+\\|")
#[1] "Apodemia_mejicanus"

If we use [A-Z] also to match it will match the upper case letter and replace with blank ("") as in the OP's str_replace_all

data

str1 <- "BLCU142-09|Apodemia_mejicanus"

Regex to get the entire string after last occurrence of # in a string

let myRegex = new RegExp(/^[^]*(#[^]*)$/);
let matches = myRegex.exec("%sdfsdf#\n##sdgsdf\ngdf#sadgofofo#jhjhj\nhjh");
let myResult = matches[1]

The pattern break down is this:

'^' start at the beginning of the string
'[^]*' match everything including line breaks as much as you can including any '#'
'(#[^]*)' group a match of the final '#' and anything following it, including line breaks
'$' to the end of the string

Regex is greedy so it will suck up everything and land on the final '#' using the pattern above.
The '[^]' is used to match on line breaks.
I learned that from the MDN site examples here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp

Here is a wonderful regular expression testing site if you have not found it yet:

https://regex101.com/

Here is a JS Fiddle of the above solution
https://jsfiddle.net/y8qbe705/

Expression after last specific character

It is one of several shell features, generically called shell expansion. This particular expansion is called parameter expansion*.

You can think of this particular shell expansion form as a left-truncate string function. You must use the curly braces as shown (that is not optional)..

When you use only one #, it means left-truncate only the first occurrence of the pattern which follows (up to the closing }. When you use two ##, it means left-truncate all consecutive pattern-matches. The result of var="a/b/c"; echo ${var#*/} is b/c... echo ${var##*/} returns c.

There is a complementary right-truncate. It uses % instead of the #... (I "remember" which is which because # is like a bash comment; always on the left).

The * is treated as a bash wildcard expansion.

Here is a list of all shell expansions, presented in precedence order.

The order of expansions is:

1. brace expansion ... prefix{-,\,}postfix             # prefix-postfix prefix,postfix
.. {oct,hex,dec,bin} # oct hex dec bin
. {a..b}{1..2} # a1 a2 b1 b2
. {1..04} # 01 02 03 04
. {01..4} # 01 02 03 04
. {1..9..2} # 1 3 5 7 9
. \$\'\\x{0..7}{{0..9},{A..F}}\' # $'\x00' .. $'\x7F'

2. tilde expansion .... ~ # $HOME
... ~axiom # $(dirname "$HOME")/axiom
... ~fred # $(dirname "$HOME")/fred
.. ~+ # $PWD (current working directory)
.. ~- # $OLDPWD (previous working directory. If OLDPWD is unset,
~- is not expanded. ie. It stays as-is,
regardless of the state of nullglob.)
# Expansion for Directories in Stack. ie.
# The list printed by 'dirs' when invoked without options
. ~+N # Nth directory in 'dirs' list (from LHS)
. ~-N # Nth directory in 'dirs' list (from RHS)

3. parameter expansion .... ${VAR/b/-dd-}
... ${TEST_MODE:-0}
.. ${str: -3:2} # note space after :
. ${#string}

4. (processed left-to-right)
variable expansion
arithmetic expansion
command substitution

▶5. word splitting # based on $IFS (Internal Field Seperator)

▷6. pathname expansion
according to options such as:
nullglob, GLOBIGNORE, ...and more

# Note: ===============
▶ 5. word splitting ↰
▷ 6. pathname expansion ↰
# ===================== ↳ are not performed on words between [[ and ]]

Regex match after last / and first underscore

You can use

library(stringr)
str_extract(string, "(?<=/)[^/]{2}(?=[^/]*$)")
## => [1] "Pe"

See the R demo and the regex demo. Details:

  • (?<=/) - a location immediately preceded with a / char
  • [^/]{2} - two chars other than /
  • (?=[^/]*$) - a location immediately preceded with zero or more chars other than / till the end of string.

How to get substring after last occurrence of character in string: Swift IOS

If you really want the characters after the last comma, you could use a regular expression:

let string = "Hamilton, A"
let regex = try! NSRegularExpression(pattern: ",\\s*(\\S[^,]*)$")
if let match = regex.firstMatch(in: string, range: string.nsRange), let result = string[match.range(at: 1)] {
// use `result` here
}

Where, in Swift 4:

extension String {

/// An `NSRange` that represents the full range of the string.

var nsRange: NSRange {
return NSRange(startIndex ..< endIndex, in: self)
}

/// Substring from `NSRange`
///
/// - Parameter nsRange: `NSRange` within the string.
/// - Returns: `Substring` with the given `NSRange`, or `nil` if the range can't be converted.

subscript(nsRange: NSRange) -> Substring? {
return Range(nsRange, in: self)
.flatMap { self[$0] }
}
}

Regex to find text after last occurence of character till another one

You might use

(?s)\bincluding:(.*\\n[*•]).*?\\n(?![*•])
  • (?s) Inline modifier to make the dot match a newline
  • \bincluding: Match including: preceded by a word boundary
  • ( Capture group 1
    • .*\\n[*•] Match till the last occurrence of \n followed by either * or •
  • ( Close group 1
  • .*?\\n Match till the first occurrence of \n

Regex demo

Or when \\n is a real newline

(?s)\bincluding:(.*\n[*•]).*?\n(?![*•])

Regex demo

For example

df["Skills"] = df["Job description"].str.extract(r"(?s)\bincluding:(.*\n[*•]).*?\n(?![*•])")

Regex to match everything after the last occurrence of a string

Sorry I did not catch you wanted after:

</p>

Using this:

.*<\/p>(.*)$

You have an array where the second element is what you look for.

Tested code:

var text = '<p>Foo bar bar bar foo</p> <p>Foo bar bar foo</p> This Foo bar foo bar <br>';
var patt = new RegExp(".*<\/p>(.*)$");
var matched = patt.exec(text);

Tester:

http://www.pythontutor.com/javascript.html#mode=edit

Result:

Sample Image

regular expression match everything after the last space with the word of max 2 or 5 characters

You might use a capturing group if you want to capture the value (or make it non capturing (?:) with a character class and an alternation using | to match either 2 word chars or match 5 times one of the listed.

^.*\s(\w{2}|[\w/-]{5})$

Regex demo

Note that \s could also match a newline.


If the / and - can not occur 2 times after each other, not at the start or end and there must be at least 1 occurrence of them:

^.*\s(\w{2}|(?=[\w/-]{5}$)\w+(?:[/-]\w+)+)$

Regex demo


Or make the second part of the string optional

^.*\s([a-zA-Z]{2}(?:[/-][a-zA-Z]{2})?)$

Regex demo



Related Topics



Leave a reply



Submit