Extracting string constant from source code in a string using regular expressions in Python
By : ThatGuy
Date : March 29 2020, 07:55 AM
|
Reqular expression - Searching a string using regex and extracting the match from the original string
By : Iresh Asanga
Date : March 29 2020, 07:55 AM
hope this fix your issue By replacing those non-digit characters, you're making your task difficult. Rather you should make a regex that extracts that part directly from the string. The issue here is, you can't directly do \\d{12}, as the digits are not contiguous. So, let's modify that part. Since you can have 0 or more non-digit characters in between, you can use - \\d\\D* instead of \\d, and apply match that 11 times, and at the end, match single digit. code :
"[a-zA-Z]{3}(\\d\\D*){11}\\d)"
String str = "Lorem ipsum XYZ1234-123456-12 lorem ipsum";
Pattern pattern = Pattern.compile("[a-zA-Z]{3}(\\d\\D*){11}\\d");
Matcher matcher = pattern.matcher(str);
if (matcher.find()) {
System.out.println(matcher.group());
}
XYZ1234-123456-12
"[a-zA-Z]{3}(\\d[\\W_]*){11}\\d"
|
Extracting capital words and extracting the last word in a string
By : George Mills
Date : March 29 2020, 07:55 AM
seems to work fine I have a df that looks like this: code :
df <- within(df, st_name <- sub(".*?([A-Z]{3,}).*", "\\1", x, perl=TRUE))
df <- within(df, st_type <- sub(".+? ([A-Z]+)$", "\\1", x, perl=TRUE))
# x y st_name st_type
#1 800 Block of MAIN ST 1.92908789 MAIN ST
#2 100 Block of CHESTNUT AV 0.02487045 CHESTNUT AV
#3 BAY ST / WELLINGTON ST -2.33411242 BAY ST
#4 LARKIN ST / ELLIS ST -1.17946144 LARKIN ST
#5 MAPLE ST / WELLINGTON ST 0.12913797 MAPLE ST
#6 MEANDERING RD / MAIN ST -0.94150930 MEANDERING ST
df$st_name <- sub(".*?([A-Z]{3,}).*", "\\1", df$x, perl=TRUE)
df$st_type <- sub(".+? ([A-Z]+)$", "\\1", df$x, perl=TRUE)
|
Searching for literal string with Select-String and extracting part of string
By : user2820611
Date : March 29 2020, 07:55 AM
I wish this help you Select-String, by default, uses the .NET regex engine. To do simple string matching, use the -SimpleMatch switch parameter: code :
Select-String -Path C:\file.inf -pattern "$DeviceID" -SimpleMatch
|
C++: Get numbers between two string delimiters and values after extracting desired integer from string using stringstrea
By : Sultana Rajia
Date : March 29 2020, 07:55 AM
I wish did fix the issue. Fix for 2nd bug added at the end. You should work on getting better with a debugger, I found your problem with one: code :
while (!ss.eof())
{
//extract the word by word from stream
ss >> temp;
//Check the given word is integer or not
if (stringstream(temp) >> found)
cout << "Number found: " << found << " " << endl;;
//no space at the end of string
temp = "";
}
while (!ss.eof())
{
//extract the word by word from stream
ss >> temp;
//Check the given word is integer or not
if (stringstream(temp) >> found)
{
cout << "Number found: " << found << " " << endl;
foundRelevantSection = true;
break; /* exits the while now */
}
//no space at the end of string
temp = "";
}
while (getline (inputFile, line) && foundRelevantSection)
{
//if no matches were found, the function returns "string::npos"
if(line.find("total") != string::npos)
{
//relevant section ends now
foundRelevantSection = false;
}
else
{
cout << line << endl;
}
}
|