The pattern that you tried first matches until the end of the string using .*
. Then at the end of the string, it tries to match an optional question mark \??
. This will succeed (because it is optional) resulting in matching the whole string for the first 3 examples.
id - ([ ^ ? \s] + )
For example
import re content = "" " id - HTRY098WE id - KNGT371WE ? witkl id - ZXV555NQE ? phnu eh - VCBG075LK "" " for item in re.findall(r 'id-([^?\s]+)', content): print(item)
Result
HTRY098WE KNGT371WE ZXV555NQE
JavaScript typed arrays
const re = /ab+c/;
const re = new RegExp('ab+c');
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}
const myRe = /d(b+)d/g;
const myArray = myRe.exec('cdbbdbsbz');
const myArray = /d(b+)d/g.exec('cdbbdbsbz');
// similar to 'cdbbdbsbz'.match(/d(b+)d/g); however,
// 'cdbbdbsbz'.match(/d(b+)d/g) outputs [ "dbbd" ]
// while /d(b+)d/g.exec('cdbbdbsbz') outputs [ 'dbbd', 'bb', index: 1, input: 'cdbbdbsbz' ]
const myRe = new RegExp('d(b+)d', 'g');
const myArray = myRe.exec('cdbbdbsbz');
Finding out whether a string contains abc could just as well be done with a call to indexOf. Regular expressions allow us to express more complicated patterns.,A regular expression is a type of object. It can be either constructed with the RegExp constructor or written as a literal value by enclosing a pattern in forward slash (/) characters.,Regular expression objects have a number of methods. The simplest one is test. If you pass it a string, it will return a Boolean telling you whether the string contains a match of the pattern in the expression.,When using the RegExp constructor, the pattern is written as a normal string, so the usual rules apply for backslashes.
A regular expression is a type of object. It can be either constructed with the RegExp
constructor or written as a literal value by enclosing a pattern in forward slash (/
) characters.
let re1 = new RegExp("abc");
let re2 = /abc/;
The second notation, where the pattern appears between slash characters, treats backslashes somewhat differently. First, since a forward slash ends the pattern, we need to put a backslash before any forward slash that we want to be part of the pattern. In addition, backslashes that aren’t part of special character codes (like \n
) will be preserved, rather than ignored as they are in strings, and change the meaning of the pattern. Some characters, such as question marks and plus signs, have special meanings in regular expressions and must be preceded by a backslash if they are meant to represent the character itself.
let eighteenPlus = /eighteen\+/;
Regular expression objects have a number of methods. The simplest one is test
. If you pass it a string, it will return a Boolean telling you whether the string contains a match of the pattern in the expression.
console.log(/abc/.test("abcde"));
// → true
console.log(/abc/.test("abxde"));
// → false
let dateTime = /\d\d-\d\d-\d\d\d\d \d\d:\d\d/;
console.log(dateTime.test("01-30-2003 15:20"));
// → true
console.log(dateTime.test("30-jan-2003 15:20"));
// → false
To invert a set of characters—that is, to express that you want to match any character except the ones in the set—you can write a caret (^
) character after the opening bracket.
let notBinary = /[^01]/;
console.log(notBinary.test("1100100010100110"));
// → false
console.log(notBinary.test("1100100010200110"));
// → true