I have a text field that returns a string of characters from 38 to 40 long. I need to just extract from the 30th character to the end.
I used .{9}$ to grab the last nine, then realized that the original strings are not a set amount of characters and only the first 29 is not needed. Everything after is the case number and is what I need. Again the number of characters needed can be anywhere from 9 to 12 long
Skip the First 29 and Extract the Rest
Here are string methods .slice(), .substring(), and .replace(). A RegEx that'll skip the first 29 characters and spaces then extract the rest is:
(?:[\s\S]{29})([\s\S]*?)
Start matching non-capture: (?:...
Any space or non-space character: [\s\S]
Twenty-nine times: {29}
End matching non-capture: ..)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Next, start matching capture: (...
Any space or non-space character: [\s\S]
Zero or more times: *
Lazily: ?
End matching capture: ...)
Demo
var str = '123456789㉈123456789㉉123456789㉊123456789㉋';
var rgx = /(?:[\s\S]{29})([\s\S]*?)/;
console.log('var str = ' + str);
console.log('--------------------------------------------------');
console.log('str.slice(29) // ' + str.slice(29));
console.log('--------------------------------------------------');
console.log('str.substring(29) // ' + str.substring(29));
console.log('--------------------------------------------------');
console.log(`var rgx = ${rgx}`);
console.log('str.replace(rgx, "$1") // ' + str.replace(rgx, '$1'));
.as-console-wrapper {
min-height: 100%;
}
.as-console-row-code.as-console-row-code {
font-size: 18px;
}
.as-console-row.as-console-row::after {
display: none;
}
you can use substring function
[YourString].substring(29)
When using Python, simply do:
string[30:]
This will automatically return the thirtiest character to the end.
^.{30}\K.*$
^ asserts position at start of the string
.{30} Quantifier — Matches exactly 30 times
\K resets the starting point of the reported match. Any previously consumed characters are no longer included in the final match
.* matches any character (except for line terminators)
$ asserts position at the end of the string, or before the line terminator right at the end of the string (if any)
Related
I defined a regular expression to check if the string only contains alphabetic characters and with length 5:
use regex::Regex;
fn main() {
let re = Regex::new("[a-zA-Z]{5}").unwrap();
println!("{}", re.is_match("this-shouldn't-return-true#"));
}
The text I use contains many illegal characters and is longer than 5 characters, so why does this return true?
You have to put it inside ^...$ to match the whole string and not just parts:
use regex::Regex;
fn main() {
let re = Regex::new("^[a-zA-Z]{5}$").unwrap();
println!("{}", re.is_match("this-shouldn't-return-true#"));
}
Playground.
As explained in the docs:
Notice the use of the ^ and $ anchors. In this crate, every expression is executed with an implicit .*? at the beginning and end, which allows it to match anywhere in the text. Anchors can be used to ensure that the full text matches an expression.
Your pattern returns true because it matches any consecutive 5 alpha chars, in your case it matches both 'shouldn't' and 'return'.
Change your regex to: ^[a-zA-Z]{5}$
^ start of string
[a-zA-Z]{5} matches 5 alpha chars
$ end of string
This will match a string only if the string has a length of 5 chars and all of the chars from start to end fall in range a-z and A-Z.
I have been trying to get a regex expression to return me the following in the following situations.
XX -> XX
XXX -> XXX
XX/XX -> XX
XX/XX/XX -> XX/XX
XXX/XXX/XX -> XXX/XXX
I had the following Regex, however they do no work.
^[^/]+ => https://regex101.com/r/xvCbNB/1
=========
([A-Z])\w+ => https://regex101.com/r/xvCbNB/2
They are close but are not there.
Any Help would be appreciated.
You want to get all text from the start till the last occurrence of a specific character or till the end of string if the character is missing.
Use
^(?:.*(?=\/)|.+)
See the regex demo and the regex graph:
Details
^ - start of string
(?:.*(?=\/)|.+) - a non-capturing group that matches either of the two alternatives, and if the first one matches first the second won't be tried:
.*(?=\/) - any 0+ chars other than line break chars, as many as possible upt to but excluding /
| - or
.+ - any 1+ chars other than line break chars, as many as possible.
It will be easier to use a replace here to match / followed by non-slash characters before end of line:
Search regex:
/[^/]*$
Replacement String:
""
Updated RegEx Demo 1
If you're looking for a regex match then use this regex:
^(.*?)(?:/[^/]*)?$
Updated RegEx Demo 2
Any special reason it has to be a regular expression? How about just splitting the string at the slashes, remove the last item and rejoin:
function removeItemAfterLastSlash(string) {
const list = string.split(/\//);
if (list.length == 1) [
return string;
}
list.pop();
return list.join("/");
}
Or look for the last slash an remove it:
function removeItemAfterLastSlash(string) {
const index = string.lastIndexOf("/");
if (index === -1) {
return string;
}
return string.splice(0, index);
}
I have a String extension:
func replaceLastWordWithUsername(_ username: String) -> String {
let pattern = "#*[A-Za-z0-9]*$"
do {
Log.info("Replacing", self, username)
let regex = try NSRegularExpression(pattern: pattern, options: NSRegularExpression.Options.caseInsensitive)
let range = NSMakeRange(0, self.characters.count)
return regex.stringByReplacingMatches(in: self, options: [], range: range, withTemplate: username )
} catch {
return self
}
}
let oldString = "Hey jess"
let newString = oldString.replaceLastWordWithUsername("#jessica")
newString now equals Hey #jessica #jessica. The expected result should be Hey #jessica
I think it's because the * regex operator will
Match 0 or more times. Match as many times as possible.
This might be causing it to also match the 'no characters at the end' in addition to the word at the end, resulting in two replacements.
As mentioned by #Code Different, if you use let pattern = "\\w+$" instead, it will only match if there are characters, eliminating the 'no characters' match.
"Word1 Word2"
^some characters and then end
^0 characters and then end
Use this regex:
(?<=\s)\S+$
Sample: https://regex101.com/r/kGnQEM/1
/(?<=\s)\S+$/g
Positive Lookbehind (?<=\s)
Assert that the Regex below matches
\s matches any whitespace character (equal to [\r\n\t\f\v ])
\S+ matches any non-whitespace character (equal to [^\r\n\t\f ])
Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
$ asserts position at the end of the string, or before the line
terminator right at the end of the string (if any)
Just change your pattern:
let pattern = "\\w+$"
\w matches any word character, i.e [A-Za-z0-9]
+ means one or more
I have a requirement to trim a leading and trailing character of a fixed length column.
Ex: I have column IdNumber which is of fixed length say 11, with below values
X3343438594
7743438534X
I want to trim the leading and trailing X, and result should look like this.
3343438594
7743438534
Try this:
Search: ^X(?=\d{11}$)|(?<=^\d{11})X$
Replace: <blank>
Regex breakdown:
^X means "start of input then X"
(?=\d{11}$) means "followed by 11 digits then end"
| means "logical OR"
(?<=^\d{11}) means "preceded by start then 11 digits"
X$ means "X then end of input"
You want to delete all matches, so replace them with nothing.
var re = /(?=^X|X$)(([A-Z])(\d{10})(\s)(\d{10})([A-Z]))/;
var str = 'X3343438594 7743438534X';
var subst = '$3$4$5';
var result = str.replace(re, subst);
alert(result);
The regex first asserts that the string should have an X at the beginning or at the end, regardless of the length of your data (not necessarily 11 characters). If that's the case, it tests for a pattern that starts with one letter, followed by 10 digits (totalling 11 characters), then a space, then ten digits followed by one letter (another 11 characters).
I want to check if the string is not empty (having whitespaces only also counts as empty). How to compose the regular expression in actionscript?
The pattern should be something like /^\s*$/ (for a single line string); ^ and $ represent the start and end of the line and \s* means match zero or more whitespace characters. For example:
var s:String = /* ... */;
var allWhitespaceOrEmpty:RegExp = /^\s*$/;
if (allWhitespaceOrEmpty.test(s))
{
// is empty or all whitespace
}
else
{
// is non-empty with at least 1 non-whitespace char
}
Perhaps a simpler way as commenter Alexander Farber points out is to check for any character except a whitespace character, which is matched by \S in regex:
var nonWhitespaceChar:RegExp = /\S/;
if (nonWhitespaceChar.test(s))
{
// is non-empty with at least 1 non-whitespace char
}