regex pattern to match the end of a string
Use the $
metacharacter to match the end of a string.
In Perl, this looks like:
my $str = 'red/white/blue';
my($last_match) = $str =~ m/.*\/(.*)$/;
Written in JavaScript, this looks like:
var str = 'red/white/blue'.match(/.*\/(.*)$/);
Regular Expression only match if String ends with target
Use an end anchor ($
):
.*\.ccf$
This will match any string that ends with .ccf
, or in multi-line mode, any line that ends with .ccf
.
Regex: match patterns starting from the end of string
You can replace the lookahead with a negated character class:
([^\/\\]+):(\d+):(\d+)\D*$
See the regex demo. Details:
([^\/\\]+)
- Group 1: one or more chars other than/
and\
:
- a colon(\d+)
- Group 2: one or more digits:
- a colon(\d+)
- Group 3: one or more digits\D*$
- zero or more non-digit chars till end of string.
Regex start searching from the end of the string (reverse)
Have a try with:
((?:_[^_\r\n]*){2})$
It matches an underscore followed by any number of any character that is not underscore or line break, all that occurs twice before the end of lien.
How to match a string's end using a regex pattern in Java?
You need to match "s", but only if it is the last character in a word. This is achieved with the boundary assertion $:
input.replaceAll("s$", " ");
If you enhance the regular expression, you can replace multiple suffixes with one call to replaceAll:
input.replaceAll("(ed|s)$", " ");
In regex, match either the end of the string or a specific character
Use:
/(&|\?)list=.*?(&|$)/
Note that when you use a bracket expression, every character within it (with some exceptions) is going to be interpreted literally. In other words, [&|$]
matches the characters &
, |
, and $
.
RegEx match a number at the end of a string
.
matches any character. You need to escape it as \.
Try this:
/-?\d+\.?\d*$/
That is:
-? // optional minus sign
\d+ // one or more digits
\.? // optional .
\d* // zero or more digits
As you can see at MDN's regex page, +?
is a non-greedy match of 1 or more, not an optional match of 1 or more.
Regex to match the beginning and the end of a string in Java
UPDATE
You want to match a sed
, so you can use a\\s+sed
if there is only whitespace between a
and sed
:
String s = "afsgdhgd gfgshfdgadh a sed afdsgdhgdsfgdfagdfhh";
Pattern pattern = Pattern.compile("a\\s+sed");
Matcher matcher = pattern.matcher(s);
while (matcher.find()){
System.out.println(matcher.group(0));
}
See IDEONE demo
Now, if there can be anything between a
and sed
, use a tempered greedy token:
Pattern pattern = Pattern.compile("(?s)a(?:(?!a|sed).)*sed");
^^^^^^^^^^^^^
See another IDEONE demo.
ORIGINAL ANSWER
The main problem with your regex is the \n
at the end. $
is the end of string, and you try to match one more character after a string end, which is impossible. Also, \\s
matches a whitespace symbol, but you need a literal s
.
You need to remove \\
s and \n
and make .
match a newline, and also it is advisbale to use *
quantifier to allow 0 symbols in-between:
pattern = "(?s)^a.*sed$";
See the regex demo
The regex matches:
^
- start of stringa
- a literala
.*
- 0 or more any characters (since(?s)
modifier makes a.
match any character including a newline)sed
- a literal letter sequencesed
$
- end of string
Regex for string ending with particular pattern
You can just use the $
anchor to match the end of the string;
re.match(r"\.[0-9]$", i)
R regex to match beginning and end of string, ignoring middle
Simply adding a match all pattern .*
between the start and end should work:
grep("^\\./xl/worksheets.*\\.xml$", myfiles)
# [1] 4 6
Related Topics
Get Function Name in JavaScript
Adding Prototype to JavaScript Object Literal
Jest: Timer and Promise Don't Work Well. (Settimeout and Async Function)
Rotating Image/Marker Image on Google Map V3
Convert an Image into Binary Data in JavaScript
Should I Refrain from Handling Promise Rejection Asynchronously
Add Cors Header to an Http Request Using Ajax
Object.Getownpropertynames VS Object.Keys
How to Use Blob Url, Mediasource or Other Methods to Play Concatenated Blobs of Media Fragments
Appending Array to Formdata and Send via Ajax
Paste an Image from Clipboard Using JavaScript
How Dangerous Is It in JavaScript, Really, to Assume Undefined Is Not Overwritten
Browserify, Babel 6, Gulp - Unexpected Token on Spread Operator
Javascript/Jquery Check Broken Links