How to put a break line in string? - c++

How can i put break line in string.
Something like this.
string var = "hey
s";
Would be something like this.
hey
s

You should just put a \n between hey and s. So:
string var = "hey\ns";

Line breaking can be achieved using Dan's advice:
string var = "hey\ns";
Note that you cannot do this the way you wanted:
string var = "hey // this is not
s"; // valid code
and it's a design choice of C++.
Older languages generally do not allow you to define multiline strings.
But, for example, Python does allow you exactly this:
someString = """
this is a
multiline
string
"""
and printing someString will give you a true multiline string.
You can forget about this when using C++, though.

A line break is encoded as the char '\n'. So just write \n into your string.

You can also do this:
string var =
"\
some text\n\
some more text\n\
and even more text\
";
and var would be equels to
some text
some more text
and even more text

you should try this
string var = "hey"."/n"."s";

Related

Parsing a string by using regex

I need to parse an input string that has the format of
AB~11111, AB~22222, AB~33333, AB~44444
into separate strings:
AB~11111, AB~22222, AB~33333, and AB~44444
Here is my attempted Regex:
range = "([^~,\n]+~[^,]+,)?";
non_delimiter = "[^,\n;]+";
range_regex = new RegExp(this.range + this.non_delimiter, 'g');
But somehow this regex would only parse the input string into
AB~11111, AB~22222 and AB~33333, AB~44444
instead of parsing the input string into individual strings.
Maybe this is missing the boat, but from your input what about something like:
AB~\d+
This should match each of the strings from the above: https://regex101.com/r/vVFDIG/1. And if there's variation (i.e., it can be other letters) then maybe something like:
[A-Z]{2}~\d+
Or whatever it would need to be but using the negative character class seems like quite a roundabout way of doing it. If that's the case, you could just do:
[^ ,]+
You should use a regex split here on ,\s*:
var input = "AB~11111, AB~22222, AB~33333, AB~44444";
var parts = input.split(/,\s*/);
console.log(parts);
If you need to check that the input also consists of CSV list of AB~11111 terms, then you may use test to assert that:
var input = "AB~11111, AB~22222, AB~33333, AB~44444";
console.log(/^[A-Z]{2}~\d{5}(?:,\s*[A-Z]{2}~\d{5})*$/.test(input));

How can I validate dynamic tab and new line delimited string using regex?

I'm trying to implement a validation form with regex where I have to check whether a string is properly Tab and New Line delimited or not. I'm using jqBootstrapValidation. Column numbers can vary from 2 to 10. Like this image below:
What regex should I use?
I believe this is what you're looking for.
([A-Za-z0-9]+[\t\n])+[a-zA-Z0-9]+
This solution will work:
var s = "string here";
var regex = /\t/g;
var lines = s.split(/\n/).length - 1;
var valid = s.match(regex).length == 2 * lines;// valid or not.
alert(valid); // true or false.
Demo: http://jsfiddle.net/VP6Uj/2

AS3 split() command

Is there a way I can split by more than one character? I do not mean a combination of characters, but an array of specific choices. For example:
s = "john is tall,sue is small";
s.split(" ");
trace(s);
The output in this circumstance would be:
'john' 'is' 'tall,sue' 'is' 'small'
However, what if I wanted to edit out the comma as well such that the output was:
'john' 'is' 'tall' 'sue' 'is' 'small'
How can I do this? I'm pretty sure it's done with regex, but I'm a little lost.
Thank you in advance!
AS3's split() method accepts a regular-expression as input, so you should be able to use the following:
var str:String = "john is tall,sue is small";
var re:RegExp = /[, ]/;
var results:Array = str.split(re);
You simply need a regular expression that will match on ',' or ' ' characters. Very simply it is:
/[, ]/g

RegExp to get any number + `;#`?

I have several numbers in a string, such as:
8;#
10;#
34;#
etc...
I wanted to erase all of these from the string, so was thinking RegExp would be my best option.
What regexp expression do I use that will identify any series of numbers followed by ;# ?
Try something like this this:
\d+;#
I don't know AS3 but I think your code should look something like this:
var r:RegExp = /\d+;#/;
var s:String = "foo 8;# bar 10;#baz 34;# bah";
var x:String = s.replace(r, "")

Using a Variable in an AS3, Regexp

Using Actionscript 3.0 (Within Flash CS5)
A standard regex to match any digit is:
var myRegexPattern:Regex = /\d/g;
What would the regex look like to incorporate a string variable to match?
(this example is an 'IDEAL' not a 'WORKING' snippet) ie:
var myString:String = "MatchThisText"
var myRegexPatter_WithString:Regex = /\d[myString]/g;
I've seen some workarounds which involve creating multiple regex instances, then combine them by source, with the variable in question, which seems wrong. OR using the flash string to regex creator, but it's just plain sloppy with all the double and triple escape sequences required.
There must be some pain free way that I can't find in the live docs or on google. Does AS3 hold this functionality even? If not, it really should.
Or I am missing a much easier means of simply avoiding this task that I'm simply naive too due to my newness to regex?
I've actually blogged about this, so I'll just point you there: http://tyleregeto.com/using-vars-in-regular-expressions-as3 It talks about the possible solutions, but there is no ideal one like you mention.
EDIT
Here is a copy of the important parts of that blog entry:
Here is a regex to strip the tags from a block of text.
/<("[^"]*"|'[^']*'|[^'">])*>/ig
This nifty expression works like a charm. But I wanted to update it so the developer could limit which tags it stripped to those specified in a array. Pretty straight forward stuff, to use a variable value in a regex you first need to build it as a string and then convert it. Something like the following:
var exp:String = 'start-exp' + someVar + 'more-exp';
var regex:Regexp = new RegExp(exp);
Pretty straight forward. So when approaching this small upgrade, that's what I did. Of course one big problem was pretty clear.
var exp:String = '/<' + tag + '("[^"]*"|'[^']*'|[^'">])*>/';
Guess what, invalid string! Better escape those quotes in the string. Whoops, that will break the regex! I was stumped. So I opened up the language reference to see what I could find. The "source" parameter, (which I've never used before,) caught my eye. It returns a String described as "the pattern portion of the regular expression." It did the trick perfectly. Here is the solution:
var start:Regexp = /])*>/ig;
var complete:RegExp = new RegExp(start.source + tag + end.source);
You can reduce it down to this for convenience:
var complete:RegExp = new RegExp(/])*>/.source + tag, 'ig');
As Tyler correctly points out (and his answer works just fine), you can assemble your regex as a string end then pass this string to the RegExp constructor with the new RegExp("pattern", "flags") syntax.
function assembleRegex(myString) {
var re = new RegExp('\\d' + myString, "i");
return re;
}
Note that when using a string to store a regex pattern, you do need to add some extra backslashes to get it to work right (e.g. to get a \d in the regex, you need to specify \\d in the string). Note also that the string pattern does not use the forward slash delimiters. In other words, the following two statements are equivalent:
var re1 = /\d/ig;
var re2 = new Regexp("\\d", "ig");
Additional note: You may need to process the myString variable to escape any backslashes it might contain (if they are to be interpreted as literal). If this is the case the function becomes:
function assembleRegex(myString) {
myString = myString.replace(/\\/, '\\\\');
var re = new RegExp('\\d' + myString);
return re;
}