• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Grep in JS - search possibilities/eventualities

Engaged ,
Nov 08, 2016 Nov 08, 2016

Copy link to clipboard

Copied

Hello!

I would like to cover up various eventualities in the search GREP with. What is wrong in my mindset?

Find:  (\\t+)([\\s|\\t|][ab]€[\\s|\\t|]\\d{1,3},\\d{2}$)

Change:  ~y$2

It should look for one or more tabs before a price. After that tyb could be: nothing, a space or another tab; after that could be the word "ab" or nothing; after that "€"; after that nothing, a space or a tab. Is that not the correct notation?

TOPICS
Scripting

Views

982

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Nov 10, 2016 Nov 10, 2016

Ok, I see. You can make your search pattern more general:

Find: \s+(?=(ab\s?)?€.+$)

Replace with ~y

Note: in (\t*\s*) again, \s matches any space, including the tab, so you can leave out \t. In addition, \s* searches for any space, including none, so it matches anything. So (\t*\s*) looks for zero or more tabs followed by zero or more spaces. And finally, there's no need for the parentheses because you don't refer back to them and you don't group them. I think you confuse brackets and parentheses:

...

Votes

Translate

Translate
New Here ,
Nov 08, 2016 Nov 08, 2016

Copy link to clipboard

Copied

Are you wrapping this is quotes?

Also, [ab] will find a and b separately.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 08, 2016 Nov 08, 2016

Copy link to clipboard

Copied

If after the tab could be nothing then use \\s?\\t? this allows there to be no spaces or tabs. You wouldn't need a character class for this so just do:

"(\\t+)(\\s?\\t?[[.ab.]]$\\s?\\t?\\d{1,3},\\d{2}$)"

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Nov 08, 2016 Nov 08, 2016

Copy link to clipboard

Copied

What does [[.ab.]] mean?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 08, 2016 Nov 08, 2016

Copy link to clipboard

Copied

Show us a screenshot (with visible metachars]? Thanks!

(^/)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Nov 09, 2016 Nov 09, 2016

Copy link to clipboard

Copied

Bildschirmfoto 2016-11-09 um 09.40.31.png

Sometimes there are prices without the word "ab", or somebody placed a tab instead a space...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Nov 08, 2016 Nov 08, 2016

Copy link to clipboard

Copied

Hi jakec42735283

Yes I did. The posting contains just the pure GREP.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Nov 08, 2016 Nov 08, 2016

Copy link to clipboard

Copied

Hi,

your goal looks like this:

looks like this

\\t+( [ \\t]?[ab]?€[ \\t]?\\d{1,3},\\d{2}$)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 08, 2016 Nov 08, 2016

Copy link to clipboard

Copied

Hello,

There's few mistakes:

(\\t+)([\\s|\\t|][ab]€[\\s|\\t|]\\d{1,3},\\d{2}$)

"|" means "OR", but ending with "|]" doesn't mean "OR nothing".

You can use "?" that means "find one or zero"

(\\t+)([\\s|\\t|][ab]€[\\s|\\t|]\\d{1,3},\\d{2}$)

You allready searched for leading tabs, and there's no need for parenthesis since you'll only use the second part.

I did some tests with Firefox, inDesign javascript and GREP :

//inDesign and Firefox don't understand "[[.ab.]]" (but "[[.xxx.]]" is documented in other programs)

alert(/\t+(\s?[[.ab.]]?€[\s|\t]?\d{1,3},\d{2})$/.test("         ab€ 123,25")); // false (browser, inDesign javascript & GREP)

alert(/\t+(\s?ab€[\s|\t]?\d{1,3},\d{2})$/.test("         ab€ 123,25")); // ok

alert(/\t+(\s?ab€|\s?€)([\s|\t]?\d{1,3},\d{2})$/.test("         ab€ 123,25")); // ok (ID javascript & GREP)

alert(/\t+(\s?ab€|\s?€)([\s|\t]?\d{1,3},\d{2})$/.test("         € 123,25")); // ok (ID javascript & GREP)

alert("         € 123,25".replace(/\t+(\s?ab€|\s?€)([\s|\t]?\d{1,3},\d{2})$/, "'$1$2'")); // ok " € 123,25"

Unless you use inDesign findGrep(), there's no need for double slashes.

You need:

\t+ // for leading tabs

(\s?ab€|\s?€) // for space or not and "ab" or not and € (you'll always have a $1)

([\s|\t]?\d{1,3},\d{2}) // tab or space and price

$ // this is the end...

// complete expression:

\t+(\s?ab€|\s?€)([\s|\t]?\d{1,3},\d{2})$

// to be replaced by:

$1$2

//if you use findGrep:

\\t+(\\s?ab€|\\s?€)([\\s|\\t]?\\d{1,3},\\d{2})$

I hope this help,

Swo

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Nov 09, 2016 Nov 09, 2016

Copy link to clipboard

Copied

Thank you.

But I do not think I´ll get problems with the browser. There are no interfereces.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 09, 2016 Nov 09, 2016

Copy link to clipboard

Copied

Don't use | in character classes. [\s|\t] is wrong. Not only because of the |, but also because \s covers \t. So instead of [\s|\t] use simply \s

This grep seems to match what you want.

\t+(ab\s)?€\s?

Peter

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Nov 10, 2016 Nov 10, 2016

Copy link to clipboard

Copied

Hi Peter.

This is how the (experimental-)lines look before the change...:

Bildschirmfoto 2016-11-10 um 08.52.43.png

... and these are the same lines after using youre script-snippet (\t+(ab\s)?€\s?):

Bildschirmfoto 2016-11-10 um 08.52.51.png

As you can see, Not every row was addressed.

I use now a longer version like this:

Bildschirmfoto 2016-11-10 um 09.13.04.png

So far, it works flawlessly. Let´s see how long...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 10, 2016 Nov 10, 2016

Copy link to clipboard

Copied

Hi,

Didn't you loose some "€" in this ?

(?=) doesnt accept complex expression, as "*+", but simpler ones as (?=ab), (?=\d)

I would use find :

\t\s?([a-z ]+€|€)\s+

replace by:

~y$1

Beginning with "\t" won't get " \tblau" (space + tab + "blau")

[a-z ]+ will get "rot " or "ab " or "rot ab "

an we need "\s+" to strip tab(s) after "€"

The replacement is "~y$1 " (with a space at the end)

Swo

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Nov 10, 2016 Nov 10, 2016

Copy link to clipboard

Copied

Hey Swo.
You are right, there less €-s than before. I've completly overlooked.

What I want is, to change the tab AFTER that word (e.g. "blau"). However, there is not one word in front of the tab in each line. So I have to focus on what is after the tab.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 10, 2016 Nov 10, 2016

Copy link to clipboard

Copied

Hum,

Did you give all possible line examples ?

If yes, I suppose there's always a " \tQUALITY" (space+tab+color) in each line

So we'll just have to ensure there's a "~y" after, instead of other spaces:

Find: "( \t[^\s]+)\s+" and replace with: "$1~y"

Second, we always need a space after € (and take the text before € if there's any):

Find: "([\u\l ]+€|€)\s+" and replace with: "$2 " (a space at the end)

The complete expression without ‘"’:

"( \t[[^\s]+)\s+([\u\l ]+€|€)\s+"

replacement:

"$1~y$2 "

Now, the misplaced "rot" is in its column.

When you can't do it in 1 search/replace, try to have the more regular text with a first search/replace, and finish with a second one, or more.

we don't mind if we can script them.

Swo.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 10, 2016 Nov 10, 2016

Copy link to clipboard

Copied

Ok, I see. You can make your search pattern more general:

Find: \s+(?=(ab\s?)?€.+$)

Replace with ~y

Note: in (\t*\s*) again, \s matches any space, including the tab, so you can leave out \t. In addition, \s* searches for any space, including none, so it matches anything. So (\t*\s*) looks for zero or more tabs followed by zero or more spaces. And finally, there's no need for the parentheses because you don't refer back to them and you don't group them. I think you confuse brackets and parentheses: (\s\t) stands for 'a space followed by a tab'; [\s\t] stands for 'a space or a tab'.

Peter

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Nov 13, 2016 Nov 13, 2016

Copy link to clipboard

Copied

LATEST

That´s pretty cool! Short and effective. "Reduced to the max" (a slogan for the Smart)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines