How to extract before "#" on email adress - dart

I'd like to extraxt string "test", which is the letters before "#",by using RegExp like following, but actually could get "test#" including "#".
How can I get only "test" without "#" ?
final getPass = "test#example.com";
final regEx = RegExp(r'(.+?)#');
print(regEx.firstMatch(getPass)?.group(0));

If using a Regexp is not mandatory for you, you could use String methods :
final getPass = "test#example.com";
print(getPass.split("#").first);

Use .group(1)
final getPass = "test#example.com";
final regEx = RegExp(r'(.+?)#');
print(regEx.firstMatch(getPass)?.group(1));

Related

Checking if a String container any of a set of Symbols in Dart

I would like to check if a string contains any of the following symbols ^ $ * . [ ] { } ( ) ? - " ! # # % & / \ , > < ' : ; | _ ~ ` + =
I tried using the following
string.contains(RegExp(r'[^$*.[]{}()?-"!##%&/\,><:;_~`+=]'))
But that does not seem to do anything. I am also not able to add the ' symbol.
Questions:
How do I check if a string contains any one of a set of symbols?
How do I add the ' symbol in my regex collection?
When writing such a RegExp pattern, you should escape the special symbols (if you want to search specifically by them).
Also, to add the ' to the RegExp, there is no straightforward way, but you could use String concatenation to work around this.
This is what the final result could look like:
void main() {
final regExp = RegExp(
r'[\^$*.\[\]{}()?\-"!##%&/\,><:;_~`+=' // <-- Notice the escaped symbols
"'" // <-- ' is added to the expression
']'
);
final string1 = 'abc';
final string2 = 'abc[';
final string3 = "'";
print(string1.contains(regExp)); // false
print(string2.contains(regExp)); // true
print(string3.contains(regExp)); // true
}
To ad both ' an " to the same string literal, you can use a multiline (triple-quoted) string.
string.contains(RegExp(r'''[^$*.[\]{}()?\-"'!##%&/\\,><:;_~`+=]'''))
You also need to escape characters which have meaning inside a RegExp character class (], - and \ in particular).
Another approach is to create a set of character codes, and check if the string's characters are in that set:
var chars = r'''^$*.[]{}()?-"'!##%&/\,><:;_~`+=''';
var charSet = {...chars.codeUnits};
var containsSpecialChar = string.codeUnits.any(charSet.contains);

Remove the first word of a string in lua

What I'm looking for is something like the following, but it only applies to the first find it gets.
str:gsub("1", "")
I'd like it to only delete the first 1 it finds OR just the first word of the string.
How would I go about doing this?
try this:
local str = "234243 232564 se42"
local str, i = str:gsub("1", "",1)
print (str,i)
str = (i>0) and str or str:gsub("^.-%s", "",1)
print (str)
only when there are spaces in the string (more than one word).

Convert Xml String with node prefixes to XElement

This is my xml string
string fromHeader= "<a:From><a:Address>http://ex1.example.org/</a:Address></a:From>";
I want to load it into an XElement, but doing XElement.Parse(fromHeader) gives me an error due to the 'a' prefixes. I tried the following:
XNamespace xNSa = "http://www.w3.org/2005/08/addressing";
string dummyRoot = "<root xmlns:a=\"{0}\">{1}</root>";
var fromXmlStr = string.Format(dummyRoot, xNSa, fromHeader);
XElement xFrom = XElement.Parse(fromXmlStr).Elements().First();
which works, but seriously, do i need 4 lines of code to do this! What is a quickest / shortest way of getting my XElement?
I found out the above 4 lines are equivalent to
XNamespace xNSa = "http://www.w3.org/2005/08/addressing";
XElement xFrom = new XElement(xNSa + "From", new XElement(xNSa + "Address", "http://ex1.example.org/"));
OR ALTERNATIVELY move the NS into the 'From' element before parsing.
var fromStr = "<a:From xmlns:a=\"http://www.w3.org/2005/08/addressing\"><a:Address>http://ex1.example.org/</a:Address></a:From>";
XElement xFrom = XElement.Parse(fromStr);

Does Google Dart have something like regex.exec()?

I read the documentation (https://api.dartlang.org/stable/1.21.1/dart-core/RegExp-class.html) but could not find I was looking for. Either I didnt understand it or I overlooked something.
I am trying to replicate the following in google dart:
var regex = /foo_(\d+)/g,
str = "text foo_123 more text foo_456 foo_789 end text",
match = null;
while (match = regex.exec(str)) {
console.log(match); // matched capture groups
console.log(match.index); // index of where match starts in string
console.log(regex.lastIndex); // index of where match ends in string
}
I also created a jsfiddle: https://jsfiddle.net/h3z88udz/
Does dart have something like regex exec()?
RegExp.allMatches looks like it does what you want.
var regex = new RegExp(r"foo_(\d+)");
var str = "text foo_123 more text foo_456 foo_789 end text";
void main() {
for (var match in regex.allMatches(str)) {
print(match);
print(match.start);
print(match.end);
}
}
https://dartpad.dartlang.org/dd1c136fa49ada4f2ad4ffc0659aab51

Openresty : getting issue with ngx.re.gsub to handle magic characters of lua

I want to replace a word into my body content from other string .
To implement this i am using ngx.re.sgub but i am getting a weird issue. ngx.re.gsub is not handling magic characters.
Example :
content1 = "HiTestHello Test how are you Testall "
_ssi = "Test"
body = "$100.00"
content2 = ngx.re.gsub(content1, _ssi, body)
ngx.print(content2)
output is
Hi.00lHelo .00 how are you .00all he.00llo .00 how are you .00all
while output should like :
Hi$100.00Hello .00 how are you .00all.
Please let me know how can i achieve this .
In ngx regex, $1, $2, etc. are variable to be captured. Try escape the $ character:
body = "$$100.00"
Wrap the body with a function also avoids it:
content1 = "HiTestHello Test how are you Testall "
_ssi = "Test"
body = "$100.00"
content2 = ngx.re.gsub(content1, _ssi, function()
return body
end, "o")
ngx.print(content2)

Resources