Character Extraction from String

How to extract certain Character from JS String

Use a regular expression to create two arrays using match. The first match will match against all none numbers, the second will match against all the numbers.

If you want integers in your operators array map over the array passing in Number for the callback.

const str = '5.14 + 4 - 36';
const operations = str.match(/[^\d \.]/g);
const operands = str.match(/\d+\.?\d*/g).map(Number);
console.log(operations, operands)

Extract letter from String characters and numbers

Replace the stuff you don't want with nothing:

String firstWord = str.replaceAll("[^a-zA-Z].*", "");

to leave only the part you want.

The regex [^a-zA-Z] means "not a letter", the everything from (and including) the first non-letter to the end is "removed".

See live demo.

extract char from string

#include <iostream>
#include <string>

int main(int,char**)
{
std::string x = "HelloHello";
x.erase(x.begin());
std::cout << x << "\n";
return 0;
}

prints

elloHello

Extract part of the strings with specific format

You might use a pattern to assert 9-21 chars to the right including the underscore, then the match the first 2 parts with the single underscore:

^(?=\\w{9,21}_[A-Z0-9])[A-Z]+_[A-Z0-9]+

Explanation

  • ^ Start of string
  • (?= Positive lookahead, assert what is to the right of the current location is
    • \\w{9,21}_[A-Z0-9] Match 9-21 word chars followed by an underscore and a char A-Z or a digit
  • ) Close the lookahead
  • [A-Z]+ Match 1+ chars A-Z
  • _ Match the first underscore
  • [A-Z0-9]+ Match 1+ chars A-Z or a digit

Regex demo | R demo

x = c('XY_ABCD101_12_ACE', 'XZ_ACC122_100_BAN', 'XT_AAEEE100_12345_ABC', 'XKY_BBAAUUU124_100')
regmatches(x, regexpr("^(?=\\w{9,21}_[A-Z0-9])[A-Z]+_[A-Z0-9]+", x, perl = TRUE))

Output

[1] "XY_ABCD101"     "XZ_ACC122"      "XT_AAEEE100"    "XKY_BBAAUUU124"

Character extraction

Assuming:

  • The '=' always exists;
  • The portion of the text you'd like to extract is an integer;
  • This integer is never more than 99 characters long;

You could try the following:

Sample Image

Formula in B1:

=-LOOKUP(1,-MID(A1,FIND("=",A1)+1,ROW($1:$99)))

How to extract the characters of a string and compare them with another string in Python?

In Python, a string is a list of characters, accessible using an index inside square brackets. For example, the 2nd character in string1 can be accessed using string1[1], the 3rd string1[2].

You can also iterate through the characters directly using in.

for c in word:
print(c)

Or you can iterate over the range and access the characters one at a time.

for c in range(len(string1)):
if string1[c] == string2[c]:
logic()

Javascript regular expression to extract characters from mid string with optional end character

You may use /S=([^&]*)/ to grab from an S= to end of line or &:

["rilaS=testingabc", "rilaS=testing123&thistest"].forEach(s =>  console.log(s.match(/S=([^&]*)/)[1]));


Related Topics



Leave a reply



Submit