spot7.org logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

Get an array with characters and them ANSI colors from an input string


What's the optimal way to to this?

Possibly not with regex, and I crashed my dev tools several times while building this, but here you go:

>>> str =
'u001b[1mu001b[38;5;231mHello
Worldu001b[0mu001b[22m'
    re =
/((?:u001b[.*?m)+)([^])([^]|)(?=.*?((?:u001b[.*?m)+)|)/
    var foo, bar = []

    while (null != (foo = str.match(re)))
      if ('' !== foo[3]) {
        if ('u001b' === foo[2])
          str = ''
        else {
          bar.push({
            'start': foo[1],
            'content': foo[2],
            'end': foo[4]
          })
          str = str.replace(re, '$1$3')
        }
      } else
        str = str.replace(re, '$3')

    bar
<<<
[{start:'u001b[1mu001b[38;5;231m',content:'H',end:'u001b[0mu001b[22m'},{start:'u001b[1mu001b[38;5;231m',content:'e',end:'u001b[0mu001b[22m'},{start:'u001b[1mu001b[38;5;231m',content:'l',end:'u001b[0mu001b[22m'},{start:'u001b[1mu001b[38;5;231m',content:'l',end:'u001b[0mu001b[22m'},{start:'u001b[1mu001b[38;5;231m',content:'o',end:'u001b[0mu001b[22m'},{start:'u001b[1mu001b[38;5;231m',content:'',end:'u001b[0mu001b[22m'},{start:'u001b[1mu001b[38;5;231m',content:'W',end:'u001b[0mu001b[22m'},{start:'u001b[1mu001b[38;5;231m',content:'o',end:'u001b[0mu001b[22m'},{start:'u001b[1mu001b[38;5;231m',content:'r',end:'u001b[0mu001b[22m'},{start:'u001b[1mu001b[38;5;231m',content:'l',end:'u001b[0mu001b[22m'},{start:'u001b[1mu001b[38;5;231m',content:'d',end:'u001b[22m'}]

To handle strings like 'u001b[1mu001b[38;5;231mHellou001b[0mu001b[22m World' also:

>>> str =
'u001b[1mu001b[38;5;231mHellou001b[0mu001b[22m
World'
    re =
/((?:u001b[.*?m)+)([^])([^]|)(?=.*?((?:u001b[.*?m)+)|)/
    var foo, bar = []

    while (null != (foo = str.match(re)))
      if ('u001b' === foo[2])
        str = str.replace(re, '$2$3')
      else {
        bar.push({
          'start': foo[1],
          'content': foo[2],
          'end': foo[4]
        })
        str = str.replace(re, '$1$3')
      }

    bar
<<<
[{start:'u001b[1mu001b[38;5;231m',content:'H',end:'u001b[0mu001b[22m'},{start:'u001b[1mu001b[38;5;231m',content:'e',end:'u001b[0mu001b[22m'},{start:'u001b[1mu001b[38;5;231m',content:'l',end:'u001b[0mu001b[22m'},{start:'u001b[1mu001b[38;5;231m',content:'l',end:'u001b[0mu001b[22m'},{start:'u001b[1mu001b[38;5;231m',content:'o',end:'u001b[22m'},{start:'u001b[1mu001b[38;5;231mu001b[0mu001b[22m',content:'',end:undefined},{start:'u001b[1mu001b[38;5;231mu001b[0mu001b[22m',content:'W',end:undefined},{start:'u001b[1mu001b[38;5;231mu001b[0mu001b[22m',content:'o',end:undefined},{start:'u001b[1mu001b[38;5;231mu001b[0mu001b[22m',content:'r',end:undefined},{start:'u001b[1mu001b[38;5;231mu001b[0mu001b[22m',content:'l',end:undefined},{start:'u001b[1mu001b[38;5;231mu001b[0mu001b[22m',content:'d',end:undefined}]

Categories : Javascript

Related to : Get an array with characters and them ANSI colors from an input string
How do you scan individual characters from each line of string in a .txt file into a 2D array?
A scanner once used can't be reset to the starting position. You have to create a new instance again. I've modified your code to possibly achieve what you're trying to do - import java.util.Scanner; import java.io.File; public class LifeGrid { public static void main(String[] args) throws Exception { Scanner scanner = new Scanner(new File("seed.txt")); int numberOfRows = 0, columns =

Categories : Java
How do I display a string from an array from user input?
To prompt the user to enter a county, and then display it (without a switch) is simple enough. You could use something like, String[] county = { "Carter", "Cocke", "Washington", "Greene", "Hawkins", "Johnson", "Sullivan", "Unicoi" }; Scanner scan = new Scanner(System.in); for (int i = 0; i < county.length; i++) { System.out.printf("%d %s%n", i + 1, county[i]); } System.out.println(

Categories : Java
Output result in sequence of colors. Input from user one by one
It seems like you are also forgetting to increment guess_counter (which seems like it should only be incremented if the guess is correct) which is actually not needed if we are storing correct guesses. Otherwise, just append to an array of guesses every time you guess correctly then print it using the join method: colors = %w(red green orange yellow blue purple) correct_guesses = [] while corr

Categories : Ruby
Adding data to array from user input String
If input is Bob Jones: e100 e95 e87 q10 q10 q8 h10 h10 h10 And you recover it with: names[i] = s.next(); examScores[e] = s.nextInt(); quizScores[q] = s.nextInt(); homeworkScores[h] = s.nextInt(); It will never work... Scores are not integers on your example (they are a String), you must parse the String and split it: String readString = s.next(); // "e99" char type = readString.charAt(0);

Categories : Java
Exception in thread "main" java.lang.NumberFormatException: For input string: "" Array
Code works absolutely fine, provided you enter only numbers. If you enter empty string, it will give you the error what you have posted. Might need to add a check for empty string. if(!x.isEmpty()){ A[i] = Integer.parseInt(x); } public class end { public static void main(String[] args) throws IOException { practice obj = new practice(); obj.test()

Categories : Java
Recently Add
How to add items to sharepoint list from a html document?
Copying App Package Folder To Isolated Storage For Windows Store App
javascript save file from (sinatra's) streaming response
Use routeParams to create templateUrl in AngularJS
resize image using javascript
How to add and remove div's (input)
Onsen UI sliding menu
BxSlider is working but there is a blank space inside div
on button click delete from datatable and also from array
Jquery changing table th tag is not working properly
Get file height width before uploading using Javascript / Asp.net
Break out of Bootstrap modal dialog if unauthenticated
Listener for click on ANY element and read particular HTML-5 custom attribute when present?
What is the best way to use this in prototype method
Get Latitue and Longitude of the center in google map when draged
Jquery multidimensional Json parse
Not able to format date
How to force y position of one branch in d3 sankey plugin?
radio button doesn't come up from JS file
Calling PHP webservice method from Javascript/jQuery
Responsive menu - parent menu with link
'click' event firing multiple times, no solutions working
How to create Cordova Blackberry (Hybrid) application
Javascript date picker multiple select
How to do the marker management in google map
JQuery/Ajax Unexpected Identifier error
AngularJS Promise error catching
Populate form from child object
Jquery popup with scrollbar and column alignment
OpenLayers3 control panel
© Copyright 2017 spot7.org Publishing Limited. All rights reserved.