Dart language, How to extract specific part from a string - flutter

I want to extract from the full String this part:
Will
full String:
"Will posted an update in the group Testing"
Another example
longerName
full String:
"longerName posted an update in the group Testing"
any help please

This could work:
String str = "Will posted an update in the group Testing";
String result = str.split("</a>")[0] + "</a>";

From the top of my head, this could work.
Regex
<a\b[^>]*>(.*?)</a>
Dart
final myString = "Will bla bla bla";
final regexp = RegExp(r'<a\b[^>]*>(.*?)</a>');
// final match = regexp.firstMatch(myString);
// final link = match.group(0);
Iterable matches = regexp.allMatches(myString);
matches.forEach((match) {
print(myString.substring(match.start, match.end));
});

Related

Strip any hashtags at the end of a string in Dart

I'm currently extracting hashtags from a string and adding them to an array but how can I remove them from the string as well provided they are located by the end?
For example:
// Random string with tags
String message = "Hello #tag1 and #tag2 the end #tag3 #tag4";
RegExp regex = RegExp(r"\B#\w\w+");
List<String> tags = [];
// Add each tag to list
regex.allMatches(message).forEach((match){
tags.add(match.group(0)!);
});
How to remove #tag3 #tag4 from the original string so it's updated to look:
Hello #tag1 and #tag2 the end
The only reason I'm keeping #tag1 and #tag2 is because it would look weird if they were taken out since it's within a sentence. Note that the string can be anything.
Original code taken from here.
Try this:
String text = "Test #tag1 and #tag2 end #tag3 #tag4";
String resultText = "";
var l = text.split(" ");
int index = l.lastIndexWhere((element) => !element.contains('#'));
l.removeRange(index + 1, l.length);
resultText = l.join(' ');
print("resultText= $resultText"); //resultText= Test #tag1 and #tag2 end

how can I check if any word of a string matches a word in a given list?

for example if I have a string = "I Like To Play Football" and a list = [Car,Ball,Door,Sky] it should give true.
Use any of list
var list = ["Car","Ball","Door","Sky"];
String text = "i like to play football";
if (list.any((item) => text.toLowerCase().contains(item))) {
//Text has a value from list
}
Here You Go:
void main() {
final String tempString = "I Like To Play Football";
final List<String> tempList = ["Car","Ball","Door","Sky"];
for(var i=0; i < tempList.length; i ++) {
if(tempString.contains(tempList.elementAt(i).toLowerCase())){
print("Found and its ${tempList[i]}");
}
}
}
Regex is your friend here. You can make a simple regex that uses each string in the array as an option (and make it case insensitive) then run the match. I've made an example here in javascript, but it's easy to do in dart
https://api.dart.dev/stable/2.16.1/dart-core/RegExp-class.html
const source = "I Like To Play Football";
const toMatch = ["Car","Ball","Door","Sky"];
let regexString = '';
for (const option of toMatch) {
//adding | modifier after string. Last one is redundant of course
//also I'm not checking for special regex characters in toMatch, but that might be necessary.
regexString += option + '|';
}
// using slice to remove last |
console.log(regexString.slice(0, -1));
const regexp = new RegExp(regexString.slice(0, -1), 'i');
console.log(source.match(regexp));
Here's a short version:
var src = 'I Like To Play Football'.split(' ');
var list = ['Car','Ball','Door','Sky'];
var result = list.any((x) => src.any((y) => y.toLowerCase().contains(x.toLowerCase())));
print(result);

How to extract number only from string in flutter?

Lets say that I have a string:
a="23questions";
b="2questions3";
Now I need to parse 23 from both string. How do I extract that number or any number from a string value?
The following code can extract the number:
aStr = a.replaceAll(new RegExp(r'[^0-9]'),''); // '23'
You can parse it into integer using:
aInt = int.parse(aStr);
const text = "23questions";
Step 1: Find matches using regex:
final intInStr = RegExp(r'\d+');
Step 2: Do whatever you want with the result:
void main() {
print(intInStr.allMatches(text).map((m) => m.group(0)));
}

Merge string item in list with another string

Is there any way to merge String item in list with another string ?
for example:
I have this list
final list1 = ['Hello'];
i need when click on button add World word to Hello word
so the list will be
['Hello World']
Use
final list1 = ['Hello']; // given
String hello = list1[0]; // get first letter
String world = 'World'; // letter you want to add
list1[0] = hello + ' ' + world; // concatenate both string and update list

Notepad++ conditional Macro

I'd like to search for a string starting with doi = { or url = { and then remove it from the file. For example, for the following data I'd like to remove the url and subsequently doi sections.
I don't know how I can use the replace command, as I don't know the complete string, and for Macro, how can I do this if these lines are not at regular distance from each other?
#article{Carrion2006,
author = {Carrion, M. and Arroyo, J.M.},
doi = {10.1109/TPWRS.2006.876672},
journal = {IEEE Trans. Power Syst.},
title = {{Bla Bla Bla 1}},
pages = {1371--1378},
url = {http://ieeexplore.ieee.org/lpdocs/epic03/wrapper.htm?arnumber=1664974},
year = {2006}
}
#article{Chandrasekaran2012,
author = {Chandrasekaran, K. and Hemamalini, S. and Simon, Sishaj P. and Padhy, Narayana Prasad},
issn = {03787796},
journal = {Electr. Power Syst. Res.},
pages = {109--119},
publisher = {Elsevier B.V.},
title = {{Bla Bla Bla 2}},
url = {http://linkinghub.elsevier.com/retrieve/pii/S0378779611002471},
volume = {84},
year = {2012}
}
How about:
Find what: ^(?:doi|url)\s*=\s*[^}]+\},\R
Replace with: NOTHING