I tried to manipulate a text to match and extract special or some words in Dart. For example:
I want to check matching of words ends with a given word
String oldText = "website or show me to";
String newText = "show me to tell";
if (oldText.trim().endsWith(newText.trim())) {
final extractText = oldText.trim().substring(newText.length);
print(extractText);
} else {
print("Not matched");
}
I want to extract only word - "tell" so how can I do it? Need to use Regular expression?
Take a look here. I've implemented a regex which will check that tell is followed by end of line $. The regex is probably easier than any other method you might try, plus it's easy to change if conditions change.
void main() {
String oldText = "website or show me to";
String newText = "show me to tell";
RegExp parser = RegExp(r"tell$");
if (parser.hasMatch(oldText)) {
print(parser.stringMatch(oldText).toString());
} else {
print("Not matched");
}
if (parser.hasMatch(newText)) {
print(parser.stringMatch(newText).toString());
} else {
print("Not matched");
}
}
And here is an example where I have removed the check to a function and also set it up so that you can dynamically generate the regex:
void main() {
String oldText = "website or show me to";
String newText = "show me to tell";
String test = "tell";
RegExp parser = RegExp(test+r"$");
print(checkMatch(oldText, parser));
print(checkMatch(newText, parser));
}
checkMatch(text, parser) {
if (parser.hasMatch(text)) {
return(parser.stringMatch(text).toString());
} else {
return("Not matched");
}
}
Related
I am making a section with TextFields and Button("Continue") and then use .disable(isValidAddress) modifier to a whole section to disable the button. The code works well, but I am seeking any solution to make it more succinct with no need to write .hasPrefix() or .hasSuffix() to all parameters one by one.
var isValidAddress: Bool {
if name.hasPrefix(" ") || street.hasPrefix(" ") || city.hasPrefix(" ") || country.hasPrefix(" ") {
return false
} else if name.hasSuffix(" ") || street.hasSuffix(" ") || city.hasSuffix(" ") || country.hasSuffix(" ") {
return false
}
return true
}
var isValidAddress: Bool {
[street, name, city, etc..].reduce(true, { result, text in
if text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
return false
} else {
return result
}
})
}
You can add them to an array and trim white space and check if empty in a loop
func isValidAddress() -> Bool {
for field in [name, street, city, country] {
if field.trimmingCharacters(in: .whitespaces).isEmpty { return false }
}
return true
}
I used a function here but a computed property works just as well.
If you don't mind moving some complexity into extensions, you can tidy up the call site. This makes it more readable, and less error prone - you can easily add in another field without mistakes for example.
extension String {
func hasPrefixOrSuffix(_ s: String) -> Bool {
hasPrefix(s) || hasSuffix(s)
}
var noSpacesAtEnds: Bool {
!hasPrefixOrSuffix(" ")
}
}
let isValid = [name, street, city, country]
.allSatisfy(\.noSpacesAtEnds)
If you do mean none of them are "all whitespace strings" then:
extension String {
var isNotAllWhitespace: Bool {
!trimmingCharacters(in: .whitespaces).isEmpty
}
}
let isValid = [name, street, city, country]
.allSatisfy(\.isNotAllWhitespace)
One benefit of having named functions for these things is you can test the pieces, so you can write a test to ensure that isNotAllWhitespace works the way you expect. You couldn't if the logic like name.hasPrefix(" ") || street.hasPrefix(" ") is mixed in with your isValidAddress function.
I started very recently to use bison for writing small compiler exercises. I am having some issues with white spaces ans comments. I was trying to debug the problem and I arrived to this source that looks like what I am looking for. I tried to chnage and erase some characters as advised but didn't work.
Also during compilation I have the following error: re2c: error: line 2963, column 0: can only difference char sets.
Below the part of the code:
yy::conj_parser::symbol_type yy::yylex(lexcontext& ctx)
{
const char* anchor = ctx.cursor;
ctx.loc.step();
// Add a lambda function to avoid repetition
auto s = [&](auto func, auto&&... params) { ctx.loc.columns(ctx.cursor - anchor); return func(params..., ctx.loc); };
%{ /* Begin re2c lexer : Tokenization process starts */
re2c:yyfill:enable = 0;
re2c:define:YYCTYPE = "char";
re2c:define:YYCURSOR = "ctx.cursor";
"return" { return s(conj_parser::make_RETURN); }
"while" | "for" { return s(conj_parser::make_WHILE); }
"var" { return s(conj_parser::make_VAR); }
"if" { return s(conj_parser::make_IF); }
// Identifiers
[a-zA-Z_] [a-zA-Z_0-9]* { return s(conj_parser::make_IDENTIFIER, std::string(anchor, ctx.cursor)); }
// String and integers:
"\""" [^\"]* "\"" { return s(conj_parser::make_STRINGCONST, std::string(anchor+1, ctx.cursor-1)); }
[0-9]+ { return s(conj_parser::make_NUMCONST, std::stol(std::string(anchor, ctx.cursor))); }
// Whitespace and comments:
"\000" { return s(conj_parser::make_END); }
"\r\n" | [\r\n] { ctx.loc.lines(); return yylex(ctx); }
"//" [^\r\n]* { return yylex(ctx); }
[\t\v\b\f ] { ctx.loc.columns(); return yylex(ctx); }
Thank you very much for pointing in the right direction or shading some lights on why this error could be solved.
You really should mention which line is line 2963. Perhaps it is here, because there seems to be an extra quotation mark in that line.
"\""" [^\"]* "\""
^
I am using AbstractFormatter2 with xtext 2.9.2
I want to put the comment in a specific column
My syntax looks something like this
terminal SL_COMMENT : '*' !('\n'|'\r')* ('\r'? '\n')?;
So far, I tried to put multiple spaces before my comment, but this doesn't work either
def dispatch void format(Model model, extension IFormattableDocument document) {
SL_COMMENTRule.prepend[space " "]
model.getEnte.format;
model.getMapset.format;
}
can anyone guide my how to format comments in general then how to put them in a specific column
comment formatting is done by commentreplacers
thus something like the following should work
override createCommentReplacer(IComment comment) {
val EObject grammarElement = comment.getGrammarElement();
if (grammarElement instanceof AbstractRule) {
val String ruleName = grammarElement.getName();
if (ruleName.startsWith("SL")) {
if (comment.getLineRegions().get(0).getIndentation().getLength() > 0) {
return new SinglelineDocCommentReplacer(comment, "//") {
override configureWhitespace(WhitespaceReplacer leading, WhitespaceReplacer trailing) {
leading.getFormatting().space = " ";
}
};
} else {
return new SinglelineCodeCommentReplacer(comment, "//") {
override configureWhitespace(WhitespaceReplacer leading, WhitespaceReplacer trailing) {
leading.getFormatting().space = " ";
}
}
}
}
}
super.createCommentReplacer(comment)
}
I'm trying to unmarshall some XML which is structured like the following example:
<player>
<stat type="first_name">Somebody</stat>
<stat type="last_name">Something</stat>
<stat type="birthday">06-12-1987</stat>
</player>
It's dead easy to unmarshal this into a struct like
type Player struct {
Stats []Stat `xml:"stat"`
}
but I'm looking to find a way to unmarshal it into a struct that's more like
type Player struct {
FirstName string `xml:"stat[#Type='first_name']"`
LastName string `xml:"stat[#Type='last_name']"`
Birthday Time `xml:"stat[#Type='birthday']"`
}
is there any way to do this with the standard encoding/xml package?
If not, can you give me a hint how one would split down such a "problem" in go? (best practices on go software architecture for such a task, basically).
thank you!
The encoding/xml package doesn't implement xpath, but does have a simple set of selection methods it can use.
Here's an example of how you could unmarshal the XML you have using encoding/xml. Because the stats are all of the same type, with the same attributes, the easiest way to decode them will be into a slice of the same type. http://play.golang.org/p/My10GFiWDa
var doc = []byte(`<player>
<stat type="first_name">Somebody</stat>
<stat type="last_name">Something</stat>
<stat type="birthday">06-12-1987</stat>
</player>`)
type Player struct {
XMLName xml.Name `xml:"player"`
Stats []PlayerStat `xml:"stat"`
}
type PlayerStat struct {
Type string `xml:"type,attr"`
Value string `xml:",chardata"`
}
And if it's something you need to transform often, you could do the transformation by using an UnamrshalXML method: http://play.golang.org/p/htoOSa81Cn
type Player struct {
XMLName xml.Name `xml:"player"`
FirstName string
LastName string
Birthday string
}
func (p *Player) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
for {
t, err := d.Token()
if err == io.EOF {
break
} else if err != nil {
return err
}
if se, ok := t.(xml.StartElement); ok {
t, err = d.Token()
if err != nil {
return err
}
var val string
if c, ok := t.(xml.CharData); ok {
val = string(c)
} else {
// not char data, skip for now
continue
}
// assuming we have exactly one Attr
switch se.Attr[0].Value {
case "first_name":
p.FirstName = val
case "last_name":
p.LastName = val
case "birthday":
p.Birthday = val
}
}
}
return nil
}
In my View I have this condition:
#if (User.Identity.Name == "abc")
{
... do this!
}
How can I split this string "User.Identity.Name" in View (in MVC) so I can create new condition, something like this:
string last = User.Identity.Name.Substring(User.Identity.Name.LastIndexOf('.') + 1);
if (last == "this")
{
... do this!
}
thanks.
you can do it like this:
#{
var temp= User.Identity.Name.Split('.');
if (temp[temp.Count() - 1] == "this")
{
}
}
or if "." will be only one time in that string then you can hardcode like this:
#{
var temp= User.Identity.Name.Split('.');
if (temp[1] == "this")
{
}
}
see below example , which finds last word of a string after last dot in it.
String str = "abc.def.xyz";
String last = str.substring(str.lastIndexOf('.')+1);
System.out.println(last);
if(last.equals("something")){
//do this
}
else{
//do this
}
here last comes as xyz