AS3 split() command - regex

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

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 to fix this regexp?

I have a regexp (,\s*?\n)(\s*?)) and according to https://regex101.com/ it should work. The only problem is that it isn't. What I want to achieve is:
'some text,
)'
will get converted to
'some text
)'
I know that if that regexp of mine would somehow work than the output string would be:
'some text)'
Is there any way to not move the ')' to the same line as 'some text'?
The sample that I used for testing:
declare
l_example varchar2(32000);
begin
l_example :='some text,
)';
dbms_output.put_line(l_example);
l_example := regexp_replace(l_example, '(,\s*?\n)(\s*?\))', '\2');
dbms_output.new_line;
dbms_output.put_line(l_example);
END;
/
Please check this:
regexp_replace(l_example, '(,\s*?'|| CHR(10)||' *)(\s*?\))', '\2', 1, 1,'m');
db<>fiddle

Matlab regex: replace comma and one letter

I have a string like this in matlab.
str='42 21 S'
How could I convert it into the following form?
str='42.21'
What I tried with regexprep() is the following:
regexprep(str,'S','');
regexprep(str,' ', '.')
which leaves me with this
str='42.21.'
This ought to do the trick, Matlab is not great with strings though so there's likely to be all sorts of ways to do it, not just using regexp/regexprep:
regexprep(regexp('42 21 A','\d+\s\d+','match'),'\s','.')
The regexp removes the space and the S at the end, and then the regexprep replaces the space with a period.
For simple replacements you don't have to use regexprep. You can use the much simpler strrep:
str = strrep(str, ' S', '');
str = strrep(str, ' ', '.');
If you require more general replacement rules, you should use regexprep, like David's answer for example.

Split line at commas, only if commas not contained between quotes

Is there any way to use the split function in scala so that it splits a line at commas but doesn't at commas contained within 2 double quotes?
For example, I have the following:
x: String = """"??", "hamburger", "ketchup, mayo, mustard", "pizza""""
and I tried this:
x.split(',') but it didn't work. I then thought about removing all double quotes but that still doesn't solve my problem.
Any help would be greatly appreciated!
EDIT:
Here's a snippet of my code to see how I can incorporate this:
val data1 = noheader1.map { line =>
val values = line._1.split(',') //This is what I am trying to change
val name = values(2).replaceAll("\"", ""))
I am a bit new to scala and even more so to regex, so could someone clarify how to write that weird regex expression in my code so that I can obtain an ARRAY of the comma separated words of the line?
Try this!
(?>"(?>\\.|[^"])*?"|(,))
Regex101
Instead of split() you can use a regular expression and findAllIn(), like such:
val x = """"??", "hamburger", "ketchup, mayo, mustard", "pizza""""
""""[^"]+"""".r.findAllIn(x).toList
This will result in, List("??", "hamburger", "ketchup, mayo, mustard", "pizza")
Note: I am using triple-quotes (""") in the example.
Perhaps not so elegant as other regex already suggested, consider the splitting element between items as ", " and so
x.split("\",\\s+\"")
Array("??, hamburger, ketchup, mayo, mustard, pizza")
Then in the resulting array, to the head "?? apply stripPrefix("\"") and to the last pizza" apply stripSuffix("\"").

How to put a break line in string?

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";