Regex match and return Word \w+ space \s

Jonforum

Veteran
Veteran
Joined
Mar 28, 2016
Messages
1,623
Reaction score
1,439
First Language
French
Primarily Uses
RMMV
Hi 


I have something like this


TXTitem =
"
#Cadre: D4 Fire
#Slot: Dice
#type: Power
";


I need to get text after #Cadre:


How to get te full text with space.


I try this


var matches = /#Cadre: (\w+)\s/.exec(TXTitem);


or this


var matches = /#Cadre: (\w+)\s+/.exec(TXTitem);


but this return me only D4, i need get D4 Fire .


Regex  is very hard to understand!


Tanks


Here the full portion of code

Code:
//get the .note item selected
var TXTitem = $dataItems[DiceSlotBig[WhatSlotcliked][3]].note;
// Find in the .note item what have after #Cadre:
var matches = /#Cadre: (\w+)\s+/.exec(TXTitem);
console.log (matches[1]); // it supose to are D4 Fire but i have D4
//Show text to screen
$gameScreen.setDTextPicture('\\OW[4] ' + matches[1]  + ' ', 24); //affiche item id .name
$gameScreen.setFont('GameFont');
$gameScreen.showPicture(57, '', 1, TempX, TempY, 0, 0, 255, 0);
this.bindPictureToMap((57), 1, 'above_characters');
 
Last edited by a moderator:

taarna23

Marshmallow Princess
Global Mod
Joined
Jul 20, 2012
Messages
2,402
Reaction score
4,966
First Language
English
Primarily Uses
RMMZ
Regex makes my head hurt on good days, and today is not one of those. However, there is hope.


Go here: http://regexr.com/


Under Expression, put your regex. Under Text, put whatever text you like and if any of that text matches your regex requirements, it will be highlighted.
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,098
Reaction score
13,704
First Language
English
Primarily Uses
RMMV
What you've got there is telling it to find text followed by a space.  You don't have anything to look for the text after the space.  And if you did, it'd return several strings rather than just one.


Try /#Cadre: ([\w\s]+)/
 

Jonforum

Veteran
Veteran
Joined
Mar 28, 2016
Messages
1,623
Reaction score
1,439
First Language
French
Primarily Uses
RMMV
What you've got there is telling it to find text followed by a space.  You don't have anything to look for the text after the space.  And if you did, it'd return several strings rather than just one.


Try /#Cadre: ([\w\s]+)/


You rock @Shaz , Tank you a lot.


So now this work and return me word + space


var matches = /#Cadre: ([\w\s]+)/.exec(TXTitem);



Regex makes my head hurt on good days, and today is not one of those. However, there is hope.


Go here: http://regexr.com/


Under Expression, put your regex. Under Text, put whatever text you like and if any of that text matches your regex requirements, it will be highlighted.


Hey! tank you @Taarachnia , very nice link.


I was almost found solution, but @Shaz give me the soluce before :)


I keep this link to my Fav 


Tank you guys
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,098
Reaction score
13,704
First Language
English
Primarily Uses
RMMV
I only found the solution because of that link :)
 

Jonforum

Veteran
Veteran
Joined
Mar 28, 2016
Messages
1,623
Reaction score
1,439
First Language
French
Primarily Uses
RMMV
your more fast than me haha .!
 

Jonforum

Veteran
Veteran
Joined
Mar 28, 2016
Messages
1,623
Reaction score
1,439
First Language
French
Primarily Uses
RMMV
What you've got there is telling it to find text followed by a space.  You don't have anything to look for the text after the space.  And if you did, it'd return several strings rather than just one.


Try /#Cadre: ([\w\s]+)/

Regex makes my head hurt on good days, and today is not one of those. However, there is hope.


Go here: http://regexr.com/


Under Expression, put your regex. Under Text, put whatever text you like and if any of that text matches your regex requirements, it will be highlighted.






For technical and dynamic reason, I'd rather pass the text to search with a variable.
I is not managed to do, how to do this as example.


I think we should call the object like this?


var new RegExp = /([\w\s]+)/;



But I'm completely lost.
If anyone can make out that the example of function down, working with the var txt passed
Tanks a lot.

Code:
function GetTXTnoteAfter(txt) {
var TXTitem = 'this is the sentence and need get thiss: blabla';
var matches = /txt ([\w\s]+)/.exec(TXTitem); /// txt are a variable with text to search = 'thiss:'
return matches;
}

GetTXTnoteAfter('thiss:'); // the text to search passed to function
var test = GetTXTnoteAfter();// get the return result of the function in var test


alert(test[1]); // show result var test, result need are  ( blabla )
console.log (test[1]); // result need are  ( blabla )
 
Last edited by a moderator:

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,098
Reaction score
13,704
First Language
English
Primarily Uses
RMMV
http://pastebin.com/07Dmbe6h


This is the More Escape Codes plugin that's shipped with MV.  In it, I let the user specify what the escape code will be to show a nickname, a face or a bust graphic.  Then I use regular expressions to build the pattern to search for - look at the definitions of reHandle and reHandlePattern on lines 96 and 97, reFace and reBust on lines 89 and 90 and their patterns on lines 189 and 190 (looking back, I don't know why I set up the patterns for the nickname in one spot and the face/bust in another).  Then lines 191 and 193 do the search for the face/bust patterns in the text, and line 121 handles the nickname replacement.
 

Jonforum

Veteran
Veteran
Joined
Mar 28, 2016
Messages
1,623
Reaction score
1,439
First Language
French
Primarily Uses
RMMV
http://pastebin.com/07Dmbe6h


This is the More Escape Codes plugin that's shipped with MV.  In it, I let the user specify what the escape code will be to show a nickname, a face or a bust graphic.  Then I use regular expressions to build the pattern to search for - look at the definitions of reHandle and reHandlePattern on lines 96 and 97, reFace and reBust on lines 89 and 90 and their patterns on lines 189 and 190 (looking back, I don't know why I set up the patterns for the nickname in one spot and the face/bust in another).  Then lines 191 and 193 do the search for the face/bust patterns in the text, and line 121 handles the nickname replacement.
tanks  @Shaz 


there is a very good example of use, I will study Some lines, to make mine.
Thank you very much.
Hope I manage to understand.



my brain is boiling


the idea of my game,items .note is actually some little notes that will be used to identify them and gave them more detail.


Capture.JPG
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,098
Reaction score
13,704
First Language
English
Primarily Uses
RMMV
It gets even easier in that case.


You can change your notes to something like this:

Code:
<Slot:Dice>
<Type:Power>
<Cadre:D4 Fire>
<DiceRun:4>
then in your code you can do this:


var item = $dataItems[128]; // if you already have a variable that contains the item object you don't need to do this
console.log(item.Slot); // this will print Dice to the console
console.log(item.Type); // this will print Power to the console
console.log(item.Cadre); // this will print D4 Fire to the console
console.log(item.DiceRun); // this will print 4 to the console

// then you can do stuff like this
if (item.Slot === 'Dice') {
// do stuff
} else {
// do other stuff
}


You don't need to bother with regular expressions at all!
 
Last edited by a moderator:

Jonforum

Veteran
Veteran
Joined
Mar 28, 2016
Messages
1,623
Reaction score
1,439
First Language
French
Primarily Uses
RMMV
It gets even easier in that case.


You can change your notes to something like this:

Code:
<Slot:Dice>
<Type:Power>
<Cadre:D4 Fire>
<DiceRun:4>
then in your code you can do this:



var item = $dataItems[128]; // if you already have a variable that contains the item object you don't need to do this
console.log(item.Slot); // this will print Dice to the console
console.log(item.Type); // this will print Power to the console
console.log(item.Cadre); // this will print D4 Fire to the console
console.log(item.DiceRun); // this will print 4 to the console

// then you can do stuff like this
if (item.Slot === 'Dice') {
// do stuff
} else {
// do other stuff
}


You don't need to bother with regular expressions at all!


OMFG !!!! lol by the hell !
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,098
Reaction score
13,704
First Language
English
Primarily Uses
RMMV
Actually, that's not quite right.


It needs to be item.meta.Slot rather than just item.Slot.  Make sure there isn't a space between Slot: and Dice - so <Slot:Dice> rather than <Slot: Dice>.


Anything you put in the note box of any object on any database tab, as well as on the map, and that little note box beside an event name, in the format <key:value> is available to use under the object's meta property.  


You can also just put <key> without a value, and it will give you a true/false value based on whether that note is present.  So in my Actor Stepping Animation plugin (if you have an actor with wings and want it to use stepping animation on the map when not moving, for example), I can put <stepanim> in the note box, then in the plugin I can do if (actor.meta.stepanim) { ... } and it will execute that block of code if that actor has <stepanim> in the note box.
 
Last edited by a moderator:

Jonforum

Veteran
Veteran
Joined
Mar 28, 2016
Messages
1,623
Reaction score
1,439
First Language
French
Primarily Uses
RMMV
Actually, that's not quite right.


It needs to be item.meta.Slot rather than just item.Slot.


Anything you put in the note box of any object on any database tab, as well as on the map, and that little note box beside an event name, in the format <key:value> is available to use under the object's meta property.  


You can also just put <key> without a value, and it will give you a true/false value based on whether that note is present.  So in my Actor Stepping Animation plugin (if you have an actor with wings and want it to use stepping animation on the map when not moving, for example), I can put <stepanim> in the note box, then in the plugin I can do if (actor.meta.stepanim) { ... } and it will execute that block of code if that actor has <stepanim> in the note box.
it a very nice feature included that I did not know.
This will remove a workload.
I'll do a test and mamusé can with these great information.
Thank you.
 
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

People3_5 and People3_8 added!

so hopefully tomorrow i get to go home from the hospital i've been here for 5 days already and it's driving me mad. I miss my family like crazy but at least I get to use my own toiletries and my own clothes. My mom is coming to visit soon i can't wait to see her cause i miss her the most. :kaojoy:
Couple hours of work. Might use in my game as a secret find or something. Not sure. Fancy though no? :D
Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.

Forum statistics

Threads
105,868
Messages
1,017,085
Members
137,583
Latest member
write2dgray
Top